Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Ambiguity between Parameterless constructor and '()' operator (http://www.go4expert.com/forums/ambiguity-parameterless-constructor-t29417/)

debugEnthu 11Jan2013 15:29

Ambiguity between Parameterless constructor and '()' operator
 
Hi,

Following is the code snippet:
Code:


    #include <stdio.h> 
    typedef double D; 
  class Action_Figure     
  {        D x;     
            public:     
                  Action_Figure( D a, D b ) { x = a * b; }       
                  Action_Figure( D a ) { x = a; }     
                  Action_Figure() { x = 5.0;
                  void operator()()        { printf("Action Level at %g\n", x); }     
  }; 

    int main()     
  {     
        Action_Figure plane_man( 2.2, 8.6 );     
        Action_Figure train_man();     
        Action_Figure truck_man( 3.3 );     
        plane_man();     
        train_man();     
        truck_man();   
        return 0;     
  }

Here , there would be ambiguity between
operator and Action_Figure() constructor
When I execute this i get the following error
( text+0x57): undefined reference to `train_man

How do i resolve this when I need both the features..

One workaround wat i thought was
Code:

  class Action_Figure
      {        D x;   
                public:   
                  Action_Figure( D a, D b ) { x = a * b; }     
                  Action_Figure( D a ) { x = a; }     
                  Action_Figure() { x = 5.0;       
                  void operator()()
                { printf("Action Level at %g\n", x); } 
        }train_man;

However, It would be great I someone could suggest a better approach

Chong 17Jan2013 23:40

Re: Ambiguity between Parameterless constructor and '()' operator
 
Hi
In your 1st code snippet,you should have
Action_Figure train_man;
instead of
Actioin_Figure train_man();
Best regards


All times are GMT +5.5. The time now is 00:33.