By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

operator() with different args in derived functor

P: 2
Hello,

I am trying to build a class which has two operator()-methods with different argument lists:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. class base
  4. {
  5.   public:
  6.     virtual double operator()() const { return 1; }
  7. };
  8.  
  9. class derived : public base
  10. {
  11.   public:
  12.     virtual double operator()( double x ) { return x;}
  13. };
  14.  
  15. int main( int argc, char* argv[] )
  16. {
  17.   derived func;
  18.   std::cout << func() << std::endl;        
  19.   std::cout << func( 5 ) << std::endl;
  20.   return 0;
  21. }
  22.  
gcc 4.1.0 aborts compilation with "error: no match for call to (derived) ()". There is no problem if the operator()(double) definition and usage is absent.

What am I doing wrong ?

Thanks in advance for your help,
Andreas
Feb 26 '07 #1
Share this Question
Share on Google+
4 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hello,

I am trying to build a class which has two operator()-methods with different argument lists:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. class base
  4. {
  5.   public:
  6.     virtual double operator()() const { return 1; }
  7. };
  8.  
  9. class derived : public base
  10. {
  11.   public:
  12.     virtual double operator()( double x ) { return x;}
  13. };
  14.  
  15. int main( int argc, char* argv[] )
  16. {
  17.   derived func;
  18.   std::cout << func() << std::endl;        
  19.   std::cout << func( 5 ) << std::endl;
  20.   return 0;
  21. }
  22.  
gcc 4.1.0 aborts compilation with "error: no match for call to (derived) ()". There is no problem if the operator()(double) definition and usage is absent.

What am I doing wrong ?

Thanks in advance for your help,
Andreas
Your error message is saying "no match for call'. This means that what you are using to pass to the fuction (your function call) does not match the definitions you have created for it. If you look, you are passing '5'. This is an int, while a double will have a remainder. Try 5.0.
Feb 26 '07 #2

P: 26
i think you just try to use function concept. you nay get through
Feb 26 '07 #3

P: 2
Thank you for your replies. I solved my problem.

The problem I had was the call func(), not func(5), and it was actually not related to the "operator()" name as I expected first.

The new operator()(double) method in "class derived" hides the operator()() method of its base class. I can avoid this feature by adding
Expand|Select|Wrap|Line Numbers
  1. using base::operator();
in "class derived".

Bye,
Andreas
Feb 26 '07 #4

P: 26
when you are dealing with class you have to use the function concept (full like c program)

all the best
Feb 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.