473,699 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::__no rmal_iterator<i nt*,
std::vector<int , std::allocator< int >, main(int, char**)::A)'


Feb 17 '08 #1
3 1766
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::__no rmal_iterator<i nt*,
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::__no rmal_iterator<i nt*,
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.go oglegroups.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.beg in(), v.end(),
std::ostream_it erator<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
2113
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 in mylist: .... setattr(myclass, item, lamdba self: func(self, item))
11
1946
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 as well as an O'Reilly one but please I am not a natural C++ programmer so allow me some scope to commit some no-brainers as it were ;)
2
1523
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
4001
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 who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
4
2145
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
6221
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
4
1379
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 comprise a portion of a much larger simulation. These classes exist in separate libraries although the library that class BkgWater is in links in the library containing the BkgLand class (sounds confusing I know). In the simulation architecture that...
14
1845
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 logical to have them all inherit from a base class that defines those, but this doesn't work: import tok class code: def __init__( self, start, stop ):
12
3011
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
28
2547
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 in advance. --dhina --------------------------------------------------------------------------------------------------------------------------------------------- class Foo { public:
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7745
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.