Simple functor

C

CodeGrommet

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_polymorphism.cpp:10
new types may not be defined in a return type
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:10
extraneous `int' ignored
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:10
`main' must return `int'
C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp
[Warning] In function `int main(...)':
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:11
no matching function for call to `mySquare::mySquare(int)'
note C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:5
candidates are: mySquare::mySquare()
note C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:5
mySquare::mySquare(const mySquare&)
********************************************************************************
 
S

Stefan Naewe

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_polymorphism.cpp:10
new types may not be defined in a return type
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:10
extraneous `int' ignored
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:10
`main' must return `int'
C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp
[Warning] In function `int main(...)':
error C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:11
no matching function for call to `mySquare::mySquare(int)'
note C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:5
candidates are: mySquare::mySquare()
note C:\unisa\MyFiles\Test_bin\test_functor_and_polymorphism.cpp:5
mySquare::mySquare(const mySquare&)
********************************************************************************

Regards,
Stefan
 
N

Neelesh Bodas

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
 
V

Victor Bazarov

Neelesh said:
missing semicolon


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
 
C

CodeGrommet

missing semicolon


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;
}
 
W

werasm

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;
}
 

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,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top