Thread: program won't work

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    program won't work

    Why doesn't the following code work when we uncomment the function comp(double& t)? Isn't the funtion supposed to get called when we do, comp c = n?

    Code:
    #include<iostream>
    #include<conio.h>
    #include<ctype.h>
    
    class comp
    {
    	double r,i;
    
    public:
    	comp (double rr = 0, double ii = 0)
    	{
    		 r = rr;
    		 i = ii;
    	}
    
    	/*comp(double &t)
    	{
    		r = t;
    		i = i;
    	}*/
    };
    
    
    int main()
    {
    	comp a(1.9,4.9), b(0.9,1.9);
    	double n = 0.1;
    	comp c = n;
    }

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by juice View Post
    Why doesn't the following code work when we uncomment the function comp(double& t)? Isn't the funtion supposed to get called when we do, comp c = n?
    Because what you're trying, needs an assignment operator.
    Like this:
    Code:
     void operator=(const double& t)
    {
         //Your code
    }

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by manasij7479 View Post
    Because what you're trying, needs an assignment operator.
    an assignment operator won't work either..

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by juice View Post
    an assignment operator won't work either..
    Really ? ...
    Then, perhaps you can enlighten me, why it is called an assignment operator !

    P.S: It works..
    Here is a small example:
    Code:
    class foo
    {
    public:
         int x;
         void operator=(int n){x=n;};
         foo(){}; 
    };

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    copy the code into your compiler, uncomment the function, introduce an overloaded assignment operator and check it out for yourself.

    You can use this assignment operator

    Code:
    void operator= (comp &t)
    {
    r = t;
    }

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by juice View Post
    copy the code into your compiler, uncomment the function, introduce an overloaded assignment operator and check it out for yourself.

    You can use this assignment operator

    Code:
    void operator= (comp &t)
    {
    r = t;
    }
    That is simply because the call becomes ambiguous with the constructor (as there are default values).
    That being the case, you don't even need an operator. The constructor will automatically be called for the assignment.(I don't know if that behaviour is standard, but my compiler seems to be satisfied.)
    Last edited by manasij7479; 02-15-2012 at 01:20 PM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your default arguments are confusing things here. How is a single argument instantiation of the class going to resolve between the two constructors? Does it call the first constructor and use a default argument of 0 for the second, or does it call the single argument constructor? If you get rid of the default arguments, the code should compile.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    But when we say comp c = n, isn't a copy constructor supposed to get invoked..

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If n was another comp object then it would call the copy constructor. But, n is a double and that particular statement gets converted into a call to the constructor that expects a double argument (of which you had two available hence the confusion).

    Code:
    comp a;
    comp b = a;  // comp b(a)
    Here, since b and a are the same type, the copy constructor would be called.

    Code:
    double n = 9.0;
    comp a = n;  // comp a(n)
    Here, since n is a double, it tries to resolve which constructor to call based on ones that accept a double parameter. You have two of them, the one with the default arguments and the one with the one double argument. Either are possible options and the compiler complains.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My c++ program does not work
    By vlan in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2011, 09:30 AM
  2. Can not get this program to work?
    By BLG in forum C Programming
    Replies: 9
    Last Post: 09-09-2009, 09:28 AM
  3. Does This Program Work For Anyone???
    By Bluesun159 in forum C++ Programming
    Replies: 18
    Last Post: 07-11-2003, 10:42 AM
  4. help getting program to work
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-13-2002, 11:04 PM
  5. Why won't my program work?
    By Unregistered in forum C Programming
    Replies: 15
    Last Post: 01-11-2002, 12:57 PM