Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code below has error C2082: redefinition of formal parameter 'm':
C++
class A {
protected:
	float Ra;
public:
	A(float a=0);
};

A::A(float a) {
	Ra = a;
}

class B: public A {
public:
	B(float r=0);
};

B::B(float r) {
	A(r); // here, I called the constructor of base class (class A)
}


Then I tried to add one more argument to the constructor of base class (class A). And in the constructor of class B, I called the constructor of class A and it worked (no error). Could anyone please tell me why?

C++
class A {
protected:
    float Ra, Rb;
public:
    A(float a=0, float b=0); // additional argument: float b
};
 
A::A(float a, float B) {
    Ra=a;
    Rb=b;
}
 
class B: public A {
public:
    B(float r=0);
};
 
B::B(float r) {
    A(r,r); // here, I called the constructor of base class (class A)
}
Posted
Comments
ARopo 19-Dec-11 5:54am    
Can't see any reason for this in the code provied , where is m the error refers to parameter m

In the first case the compiler assumes A(r) is the functional syntax of the cast operator. You have to replace A(r) with A::A(r), as in
C++
B::B(float r)
{
  A::A(r)
}


or do what we, 'standard humans', usually do
C++
B::B(float r) : A(r) 
{
}
 
Share this answer
 
Comments
Timberbird 19-Dec-11 6:28am    
Oh I see. Forgout about cast in my solution
If you want to call base class constructor, you should do it this way:
C++
B::B(float r) : A(r) {
...
}


I'm not sure what causes an error in your first example, but I can assume that compiler treats
C++
B::B(float r) {
	A(r); 
}

as an attempt to initialize new variable r of type A. This variable name conflicts with constructor's argument name, and thus you get an error of parameter redefinition (float r being redefined as A r)
 
Share this answer
 
Thanks very much for your solutions but I really want to know how the compiler worked! Thanks again :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900