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

operator() with different args in derived functor

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
4 1735
sicarie
4,677 Expert Mod 4TB
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
uppili
26
i think you just try to use function concept. you nay get through
Feb 26 '07 #3
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
uppili
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

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

Similar topics

2
by: Charles-Antoine Giuliani | last post by:
Is it possible to overload multiple times operator() ? For example for using one or two arguments ? My visual c++ does not seem to like it : I was trying to implement an arbitrary integer...
6
by: Michal Wyrebski | last post by:
Hello, I'm new in this group and I don't know if my questions are too silly, but I'm intermediate programmer and don't have enought experience. Please be charitable. I don't know how to write...
1
by: sarathy | last post by:
Hi, I have few doubts in operator overloading. Overloading is fine for operators such as +, * , - , / ....... But why would anyone overload operators ( ) -I dont see any real use when these are...
2
by: Frederic Rentsch | last post by:
Hi all, I have a class Time_Series derived from list. It lists days and contains a dictionary of various Lists also derived from list which contain values related to said days. (e.g. Stock...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
2
by: aaragon | last post by:
Hi everyone, Can someone point me out why I can't declare the operator() of a functor as static? The reason behind this is that I want to be able to call to the function without instantiating...
14
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
1
by: Stuart Brockman | last post by:
Hi, I don't quite get what is going on in this code example: --------------------------- #include <iostream> using namespace std; class Base{ public:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.