错误C2082的帮助:重新定义形式参数 [英] Help with error C2082: redefinition of formal parameter
问题描述
我下面的代码有错误C2082:重新定义形式参数"m":
My code below has error C2082: redefinition of formal parameter ''m'':
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)
}
然后,我尝试向基类(类A)的构造函数添加另一个参数.在类B的构造函数中,我调用了类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?
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)
}
推荐答案
在第一种情况下,编译器假定A(r)
是强制转换运算符的 functional 语法.您必须将A(r)
替换为A::A(r)
,如
In the first case the compiler assumesA(r)
is the functional syntax of the cast operator. You have to replaceA(r)
withA::A(r)
, as in
B::B(float r)
{
A::A(r)
}
或做我们通常做的"标准人"
or do what we, ''standard humans'', usually do
B::B(float r) : A(r)
{
}
如果要调用基类构造函数,则应采用以下方式:
If you want to call base class constructor, you should do it this way:
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
B::B(float r) {
A(r);
}
作为尝试初始化类型为A
的新变量r
的尝试.此变量名与构造函数的参数名冲突,因此会出现参数重新定义错误(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
)
非常感谢您的解决方案,但我真的很想知道编译器是如何工作的!再次感谢:)
Thanks very much for your solutions but I really want to know how the compiler worked! Thanks again :)
这篇关于错误C2082的帮助:重新定义形式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!