question about local class in a function

N

Nan Li

Hello,
Can any one explain why the following code cannot get compiled ??
Thanks.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
struct A
{
void operator()(int i)
{
cout << i << endl;
}
};

vector<int> v(3);

for_each( v.begin(), v.end(), A() );

return 0;
}

c.cpp: In function 'int main(int, char**)':
c.cpp:21: error: no matching function for call to
'for_each(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, main(int, char**)::A)'
 
I

Ian Collins

Nan said:
Hello,
Can any one explain why the following code cannot get compiled ??
Thanks.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
struct A
{
void operator()(int i)
{
cout << i << endl;
}
};

vector<int> v(3);

for_each( v.begin(), v.end(), A() );

return 0;
}

c.cpp: In function 'int main(int, char**)':
c.cpp:21: error: no matching function for call to
'for_each(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, main(int, char**)::A)'
gcc trying to tell you in its own obscure way that you can't use a local
type as a template argument.
 
S

Salt_Peter

Hello,
Can any one explain why the following code cannot get compiled ??
Thanks.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
struct A
{
void operator()(int i)
{
cout << i << endl;
}
};

vector<int> v(3);

for_each( v.begin(), v.end(), A() );

return 0;

}

c.cpp: In function 'int main(int, char**)':
c.cpp:21: error: no matching function for call to
'for_each(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, main(int, char**)::A)'

Your operator expects a parameter.
Since you'ld end up using a placeholder...
Why not just use boost::lambda

#include <iostream>
#include <vector>
#include <algorithm>
#include "boost/lambda/lambda.hpp"

int main()
{
using boost::lambda::_1;
std::vector< int > v(3, 9);
std::for_each( v.begin(), v.end(), std::cout << _1 << '\n' );
}

/*
9
9
9
*/

http://www.boost.org/doc/html/lambda.html
 
J

Jerry Coffin

Hello,
Can any one explain why the following code cannot get compiled ??
Thanks.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
struct A
{
void operator()(int i)
{
cout << i << endl;
}
};

vector<int> v(3);

for_each( v.begin(), v.end(), A() );

return 0;
}

Local types don't have linkage, so they can't be used as template
parameters.

Fortunately, the entire type and for_each that uses it work out to:

std::copy(v.begin(), v.end(),
std::eek:stream_iterator<int>(std::cout, "\n"));
 
J

Jim Langston

Nan said:
Hello,
Can any one explain why the following code cannot get compiled ??
Thanks.

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
struct A
{
void operator()(int i)
{
cout << i << endl;
}
};

vector<int> v(3);

for_each( v.begin(), v.end(), A() );

return 0;
}

c.cpp: In function 'int main(int, char**)':
c.cpp:21: error: no matching function for call to
'for_each(__gnu_cxx::__normal_iterator<int*, std::vector<int,
std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*,
std::vector<int, std::allocator<int> > >, main(int, char**)::A)'

Microsoft C++ .net 2003 gives a better error:
error C2918: '(int,char *[])main::A' : illegal use of local type in template
instantiation

Rearranging the program to:


#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

struct A
{
void operator()(int i)
{
cout << i << endl;
}
};

int main(int argc, char* argv[])
{

vector<int> v(3);

for_each( v.begin(), v.end(), A() );

return 0;
}

compiles with two warnings:

warning C4100: 'argv' : unreferenced formal parameter
warning C4100: 'argc' : unreferenced formal parameter

Because of these errors I never use a local structure or class, they can't
be used for templates which include the stl containers.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top