Redefinition of default parameter

This is a discussion on Redefinition of default parameter within the C++ Programming forums, part of the General Programming Boards category; I keep getting the error when trying to build the following code: Code: #include <iostream> using namespace std; class complexClass ...

  1. #1
    843
    843 is offline
    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,434
    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%.

  3. #3
    843
    843 is offline
    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 12:27 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,434
    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%.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    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
    843
    843 is offline
    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,434
    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%.

  8. #8
    843
    843 is offline
    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, 06: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21