473,320 Members | 2,133 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,320 software developers and data experts.

Simple functor

Hi. Please excuse this post. I searched functor in this group and
had around 1600 hits. The first page couldn't help me. Would someone
be so kind as to look at this code and tell me why it won't compile?
I'm using a mingW compiler.

******************************code**************** ****************************
#include <iostream>
using namespace std;

class mySquare
{
public:
int operator()(int x) const { return x * x; }
}

int main(){
cout<<mySquare(2)<<endl;
return 0;
}
************************************************** ******************************
******************************error
messages********************************
************************************************** ******************************
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:10
new types may not be defined in a return type
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:10
extraneous `int' ignored
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:10
`main' must return `int'
C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp
[Warning] In function `int main(...)':
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:11
no matching function for call to `mySquare::mySquare(int)'
note C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:5
candidates are: mySquare::mySquare()
note C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:5
mySquare::mySquare(const mySquare&)
************************************************** ******************************

Aug 21 '07 #1
5 1642
On Aug 21, 4:59 pm, CodeGrommet <rick.sof...@gmail.comwrote:
Hi. Please excuse this post. I searched functor in this group and
had around 1600 hits. The first page couldn't help me. Would someone
be so kind as to look at this code and tell me why it won't compile?
I'm using a mingW compiler.

******************************code**************** *****************************
#include <iostream>
using namespace std;

class mySquare
{
public:
int operator()(int x) const { return x * x; }

}
missing semicolon
int main(){
cout<<mySquare(2)<<endl;
non-static function should only be called on object. Here you are
trying to invoke a non-existant unary constructor of mySquare class.

Do this:

mySquare m;
cout<< m(2) <<endl;

Or this:

cout<< mySquare()(2) <<endl;
-N

Aug 21 '07 #2
Neelesh Bodas wrote:
On Aug 21, 4:59 pm, CodeGrommet <rick.sof...@gmail.comwrote:
>Hi. Please excuse this post. I searched functor in this group and
had around 1600 hits. The first page couldn't help me. Would
someone be so kind as to look at this code and tell me why it won't
compile? I'm using a mingW compiler.

******************************code*************** ******************************
#include <iostream>
using namespace std;

class mySquare
{
public:
int operator()(int x) const { return x * x; }

}

missing semicolon
>int main(){
cout<<mySquare(2)<<endl;

non-static function should only be called on object. Here you are
trying to invoke a non-existant unary constructor of mySquare class.

Do this:

mySquare m;
cout<< m(2) <<endl;

Or this:

cout<< mySquare()(2) <<endl;
There is another possibility. Redefine your class to have a c-tor
and a conversion function:

class mySquare {
int x;
public:
mySquare(int x) : x(x) {}
operator int() const { return x*x; }
};

and you should be able to use the syntax you had:

cout << mySquare(2) << endl;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 21 '07 #3
On Aug 21, 2:05 pm, Neelesh Bodas <neelesh.bo...@gmail.comwrote:
On Aug 21, 4:59 pm, CodeGrommet <rick.sof...@gmail.comwrote:
Hi. Please excuse this post. I searched functor in this group and
had around 1600 hits. The first page couldn't help me. Would someone
be so kind as to look at this code and tell me why it won't compile?
I'm using a mingW compiler.
******************************code**************** *****************************
#include <iostream>
using namespace std;
class mySquare
{
public:
int operator()(int x) const { return x * x; }
}

missing semicolon
int main(){
cout<<mySquare(2)<<endl;

non-static function should only be called on object. Here you are
trying to invoke a non-existant unary constructor of mySquare class.

Do this:

mySquare m;
cout<< m(2) <<endl;

Or this:

cout<< mySquare()(2) <<endl;

-N
Yes, I got. Thanks for the reply. The working code looks like this:

#include <iostream>
using namespace std;

class mySquare
{
public:
int operator()(int x) const { return x * x; }
};

int main(){
mySquare s;
cout<<s(2)<<endl;
return 0;
}

Aug 21 '07 #4
On 8/21/2007 1:59 PM, CodeGrommet wrote:
Hi. Please excuse this post. I searched functor in this group and
had around 1600 hits. The first page couldn't help me. Would someone
be so kind as to look at this code and tell me why it won't compile?
I'm using a mingW compiler.

******************************code**************** ****************************
#include <iostream>
using namespace std;

class mySquare
{
public:
int operator()(int x) const { return x * x; }
}
Missing ;
>
int main(){
cout<<mySquare(2)<<endl;
cout << mySquare()(2) << endl;
return 0;
}
************************************************** ******************************
******************************error
messages********************************
************************************************** ******************************
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:10
new types may not be defined in a return type
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:10
extraneous `int' ignored
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:10
`main' must return `int'
C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp
[Warning] In function `int main(...)':
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:11
no matching function for call to `mySquare::mySquare(int)'
note C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:5
candidates are: mySquare::mySquare()
note C:\unisa\MyFiles\Test_bin\test_functor_and_polymor phism.cpp:5
mySquare::mySquare(const mySquare&)
************************************************** ******************************
Regards,
Stefan
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Aug 21 '07 #5

Victor Bazarov wrote:

There is another possibility. Redefine your class to have a c-tor
and a conversion function:

class mySquare {
int x;
public:
mySquare(int x) : x(x) {}
operator int() const { return x*x; }
};
Alternatively, if wary of possible wrong use of user
defined conversion, free functions can be used
to achieve the same effect (almost):

#include <iostream>
class mySquare
{
public:
explicit mySquare( int x ): xSquared_( x*x ){ }
int operator()() const { return xSquared_; }
private:
int xSquared_;
};

int ToInt( const mySquare& square )
{
return square();
}

int main()
{
std::cout << ToInt(mySquare(2)) << std::endl;
}

Aug 21 '07 #6

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

Similar topics

2
by: Sean | last post by:
#include <iostream> #include <algorithm> struct functor { ~functor() {std::cout << 'D';} void operator()(int a) {std::cout << a;} }; int main() { int a = {1,2,3,4,5}; functor f;
6
by: Gert Van den Eynde | last post by:
Hi all, I'm struggling a bit with Functors generated for an ABC. This is the functor code: class Functor{ public: virtual double operator(double x)=0 }
3
by: CoolPint | last post by:
I have implemented a generic priority queue below and tested it works fine, but I have one small problem I cannot understand. I have type parameter F which determines the priority so that users can...
8
by: Amit | last post by:
Hello all. If I want to use an object both as a Functor and also, if I pass a function pointer, how can it be done ? For instance, I have something like this template< typename Iter, typename...
8
by: daniel.w.gelder | last post by:
Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task. ...
12
by: aaragon | last post by:
Hi everyone, I'm trying to provide some external functionality to a class through a functor object defined by the user. The concept is as follows: template <class Functor> class ClassA {...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
5
by: Fei Liu | last post by:
Hello, I have a situation where I need to design a library for multi-thread application, each thread does some work in a client supplied std::ptr_fun(free_function) or a functor. Now since it's...
3
by: alan | last post by:
Hello all, I'd like to know if there is a nice method of defining a functor creator which accepts an N-ary function and returns a functor based on that function. For example I have a function:...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.