Thread: Redefinition of default parameter

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Redefinition of default parameter

    I keep getting the error when trying to build the following code:

    Code:
    #include <iostream>
    using namespace std;
    
    class complexClass {
    	double x, y;
    
    public:
    	complexClass();
    	complexClass(double r, double i=0);
    	complexClass(complexClass &cNum){};
    	~complexClass(){};
    };
    
    complexClass::complexClass () {
    	x = 0; y = 0;
    }
    
    complexClass::complexClass(double r, double i=0) {
        x = r; y = i;
    }
    In addition, how do I call the constructor with &cNum? Any help is very much appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Take out the default value when defining (i.e. implementing) the constructor that accepts two doubles.

    It is also a good idea to use initialiser lists rather than assignments in the body of the constructor. For example;
    Code:
    complexClass::complexClass(double r, double i) : x(r), y(i)
    {}
    You don't generally call a copy constructor (which is what your constructor with &cNum is). The compiler does, implicitly, when you do this;
    Code:
    int main()
    {
        complexClass first(2.0, 3.0);
        complexClass second(first);    // copy constructor invoked here
    }
    Also, copy constructors usually accept a const argument: creating a copy of an object does not usually mean changing the object being copied.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Many thanks!

    What if I wanted to have the variable i set to 0 when the only one parameter is input? Is there an equivalent shortcut or should I just create another constructor?
    Last edited by 843; 03-13-2011 at 01:27 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Use default values only in the struct definition, not in the (separate) implementation of constructors.

    In other words, only specify the default value of arguments where it matters to users of the class.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You really should implement that copy constructor or delete it. As it is now, you can't make copies of your objects--and what's worse, the compiler won't complain about it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    In other words, only specify the default value of arguments where it matters to users of the class.
    Sorry, I don't quite understand what you mean. Would you mind showing an example? I want the code to set x = r and y = 0 when constructing with a single parameter.

    Please help! There is something wrong with calculation of y. It keeps returning NaN.

    Code:
    #include <iostream>
    using namespace std;
    
    class complexClass {
    	double x, y;
    
    public:
    	complexClass(): x(0), y(0) {}
    	complexClass(double r): x(r), y(0) {}
    	complexClass(double r, double i): x(r), y(i) {}
    	complexClass(complexClass &cNum): x(cNum.x) {}
    	~complexClass(){}
    
    	void operator= (complexClass a) {x=a.x; y=a.y;};
    	complexClass operator+ (complexClass a);
    	complexClass operator- (complexClass a);
    };
    
    complexClass complexClass::operator+ (complexClass a) {
    	complexClass temp(a.x+x,a.y+y);
    	return temp;
    }
    
    complexClass complexClass::operator- (complexClass a) {
    	complexClass temp(x-a.x,y-a.y);
    	return temp;
    }
    
    void complexClass::display () {
    	cout    << x << (y>=0?" + ":" - ")
                << y << "i" << endl;
    }
    
    int main (void) {
    	complexClass a (3,3);
    	complexClass b (5);
    	complexClass result;
    
    	complexClass copy(a);
    
        result = a + b;
    	result.display();
        result = a - b;
    	result.display();
    
    	return 0;
    }
    Last edited by 843; 03-13-2011 at 10:24 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by 843 View Post
    Sorry, I don't quite understand what you mean. Would you mind showing an example?
    Use the default argument in the class definition
    Code:
    class complexClass {
    	double x, y;
    
    public:
    	complexClass(double r = 0, double i = 0);
    and not in the function definition
    Code:
    complexClass::complexClass(double r, double i) : x(r), y(r)
    {}
    If you inline the constructor, do it this way.
    Code:
    class complexClass {
    	double x, y;
    
    public:
    	complexClass(double r = 0, double i = 0) : x(r), y(r) {};
    and obviously don't have a second not-inline definition.


    Quote Originally Posted by 843 View Post
    Please help! There is something wrong with calculation of y. It keeps returning NaN.
    Your copy constructor is not copying the value of y. Since your operator=() and operator+() functions accept arguments by value, the copy constructor is invoked whenever they are called. It will also possibly invoked when the functions return (although the compiler is allowed to avoid invoking the copy constructor in that case, it might invoke it anyway).

    As Elysia said, either implement the copy constructor correctly, or don't implement it at all. (If you don't implement it, the compiler will, and the compiler-supplied default will work in this case).

    Also note that,

    1) If you don't need to implement a copy constructor, you usually don't need to implement an assignment operator (operator=()).

    2) If you do need to implement operator=() or operator+() is is usually better if they accept arguments by const reference (in your case "const complexClass &"), not by value.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conflicting winsock.h and winsock2.h
    By g4j31a5 in forum Windows Programming
    Replies: 5
    Last Post: 12-29-2010, 07:03 AM
  2. Default arguments
    By swgh in forum C Programming
    Replies: 5
    Last Post: 06-29-2007, 06:27 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Consequence of const-ifying a function parameter
    By hzmonte in forum C Programming
    Replies: 3
    Last Post: 08-24-2006, 01:28 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM