Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about local class in a function

Reply
Thread Tools

question about local class in a function

 
 
Nan Li
Guest
Posts: n/a
 
      02-17-2008
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)'




 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-17-2008
Nan Li wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      02-17-2008
On Feb 17, 2:55 am, Nan Li <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      02-17-2008
In article <33e8504b-bca1-411f-bf19-b619a4846c67
@q70g2000hsb.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> 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:stream_iterator<int>(std::cout, "\n"));

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      02-18-2008
Nan Li wrote:
> 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.

--
Jim Langston
(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template function within class template not not resolving when passedfunction local class? jrwats C++ 1 03-01-2010 08:30 AM
Can local function access local variables in main program? Sullivan WxPyQtKinter Python 10 11-08-2007 02:51 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments