problem with stl compose1

F

fightwater

i wrote a program which uses the stl compose1 functor
but the compiler seems cannot find it
here is the code
//////////////////////////////////////////

#include <vector>
#include <numeric>
#include <ext/numeric>
#include <algorithm>
#include <ext/algorithm>
#include <functional>
#include <ext/functional>
using namespace std;

int main()
{
int n=10;
vector<int> aa(n);
iota(aa.begin(),aa.end(),2);
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));

vector<int>::iterator
new_end=remove_if(aa.begin(),aa.end(),compose1(
bind2nd(equal_to<int>(),0),
bind2nd(modulus<int>(),2)));
aa.erase(new_end,aa.end());
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));
return 0;
}

////////////////////////////////////////////////////////
above is the code


tostrem.cc: In function 'int main()':
tostrem.cc:20: error: 'compose1' was not declared in this scope

above is the compiler information from the command g++ tostream.cc

It's strange, since I went to the file /ext/functional , it does have
compose1 there?

Does anybody know why?
 
T

Thomas Tutone

fightwater said:
i wrote a program which uses the stl compose1 functor
but the compiler seems cannot find it
here is the code
//////////////////////////////////////////

#include <vector>
#include <numeric>
#include <ext/numeric>
#include <algorithm>
#include <ext/algorithm>
#include <functional>
#include <ext/functional>
using namespace std;

int main()
{
int n=10;
vector<int> aa(n);
iota(aa.begin(),aa.end(),2);
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));

vector<int>::iterator
new_end=remove_if(aa.begin(),aa.end(),compose1(
bind2nd(equal_to<int>(),0),
bind2nd(modulus<int>(),2)));
aa.erase(new_end,aa.end());
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));
return 0;
}

////////////////////////////////////////////////////////
above is the code


tostrem.cc: In function 'int main()':
tostrem.cc:20: error: 'compose1' was not declared in this scope

above is the compiler information from the command g++ tostream.cc

It's strange, since I went to the file /ext/functional , it does have
compose1 there?

Does anybody know why?

There is no "compose1" functor in the C++ Standard Library, so the
compiler is correct.

<OT/>

gcc includes some SGI extensions, including compose1, as extensions.
Because they are not part of the Standard Library, gcc puts them in
namespace __gnu_cxx. Accordingly, if you add "using namespace
__gnu_cxx;" to the program, you should be able to access
__gnu_cxx::compose1. You're on your own for how to use it, though,
since it's not standard.

</OT>

Best regards,

Tom
 
K

Kai-Uwe Bux

fightwater said:
i wrote a program which uses the stl compose1 functor
but the compiler seems cannot find it
here is the code
//////////////////////////////////////////

#include <vector>
#include <numeric>
#include <ext/numeric>
#include <algorithm>
#include <ext/algorithm>
#include <functional>
#include <ext/functional>
using namespace std;

int main()
{
int n=10;
vector<int> aa(n);
iota(aa.begin(),aa.end(),2);
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));

vector<int>::iterator
new_end=remove_if(aa.begin(),aa.end(),compose1(
bind2nd(equal_to<int>(),0),
bind2nd(modulus<int>(),2)));
aa.erase(new_end,aa.end());
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));
return 0;
}

////////////////////////////////////////////////////////
above is the code


tostrem.cc: In function 'int main()':
tostrem.cc:20: error: 'compose1' was not declared in this scope

above is the compiler information from the command g++ tostream.cc

It's strange, since I went to the file /ext/functional , it does have
compose1 there?

Does anybody know why?

Whatever those ext/... headers are, they are not part of the standard. So
why would they dump their stuff into namespace std? Probably, your are
dealing with some extensions provided by either by your compiler vendor or
some third party. You should find a forum dedicated to that library and
people over there might know which namespace contains compose1. (A guess
form the pathname would be ext::compose1.)


Best

Kai-Uwe Bux
 
D

Daniel T.

fightwater said:
i wrote a program which uses the stl compose1 functor
but the compiler seems cannot find it
here is the code
//////////////////////////////////////////

#include <vector>
#include <numeric>
#include <ext/numeric>
#include <algorithm>
#include <ext/algorithm>
#include <functional>
#include <ext/functional>
using namespace std;

int main()
{
int n=10;
vector<int> aa(n);
iota(aa.begin(),aa.end(),2);
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));

vector<int>::iterator
new_end=remove_if(aa.begin(),aa.end(),compose1(
bind2nd(equal_to<int>(),0),
bind2nd(modulus<int>(),2)));
aa.erase(new_end,aa.end());
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));
return 0;
}

////////////////////////////////////////////////////////
above is the code


tostrem.cc: In function 'int main()':
tostrem.cc:20: error: 'compose1' was not declared in this scope

above is the compiler information from the command g++ tostream.cc

It's strange, since I went to the file /ext/functional , it does have
compose1 there?

Does anybody know why?

'compose1' is not part of the standard library. If your functional
include has it, it probably either has some sort of conditional
compilation removing it, or doesn't put it in the std namespace.
 
F

fightwater

Thomas Tutone 写é“:
There is no "compose1" functor in the C++ Standard Library, so the
compiler is correct.

<OT/>

gcc includes some SGI extensions, including compose1, as extensions.
Because they are not part of the Standard Library, gcc puts them in
namespace __gnu_cxx. Accordingly, if you add "using namespace
__gnu_cxx;" to the program, you should be able to access
__gnu_cxx::compose1. You're on your own for how to use it, though,
since it's not standard.

</OT>

Best regards,

Tom

ok, I see.
the compose1 is under namespace __gnu_cxx
If I using __gnu_cxx, it will compile fine.

But I have strange question to ask here.
the iota function, which is define in <ext/numeric>, is also under
namespace __gnu_cxx.
Why I can use it without using namespace __gnu_cxx

here is the code I tested the iota function
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <ext/numeric>
using namespace std;

int main()
{
int n=10;
vector<int> aa(n);
iota(aa.begin(),aa.end(),2);
copy(aa.begin(),aa.end(),ostream_iterator<int>(cout,"\n"));

return 0;
}
///////////////////////////////////////////////////////////////////////////
it compiles fine, and works fine.

Can anybody explain that to me?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top