473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

question about local class in a function

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<intv(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)'


Feb 17 '08 #1
3 1755
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<intv(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.
Feb 17 '08 #2
On Feb 17, 2:55 am, Nan Li <nan.l...@gmail.comwrote:
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<intv(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
Feb 17 '08 #3
In article <33e8504b-bca1-411f-bf19-b619a4846c67
@q70g2000hsb.googlegroups.com>, na******@gmail.com 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<intv(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::ostream_iterator<int>(std::cout, "\n"));

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Ted Lilley | last post by:
What I want to do is pre-load functions with arguments by iterating through a list like so: >>>class myclass: .... pass >>>def func(self, arg): .... print arg >>>mylist = >>>for item...
11
by: Daniel Wilcox | last post by:
I have a question, I have been given a piece of code that apparantly compiles under Visual C++ but I cannot get to compile under g++ 3.2. I have read the FAQ and delved into the Stroustrup book...
2
by: Victor Liu | last post by:
hi, why n1 in local::f() is no allowed ? int n0; void function() { int n1; static int n2; class local {
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
4
by: cwc5w | last post by:
I have two classes. One with a regular destructor and the other with a virtual destructor. e.g. class x { ~x(){} } vs
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
4
by: BSand0764 | last post by:
Apologies for the length of this message, but I'm having problems getting an alternate function to be executed via a functor implementation. I have two classes (BkgLand and BkgWater) that...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
28
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.