C++ 编译器抱怨我的默认参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6210450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The compiler is complaining about my default parameters?
提问by Christian
I'm having trouble with this piece of code , after i took this class from the main.cpp file and splitted it in to .h and .cpp the compiler started complaining about the default parameters i was using in a void.
我在处理这段代码时遇到了问题,在我从 main.cpp 文件中取出这个类并将其拆分为 .h 和 .cpp 之后,编译器开始抱怨我在无效中使用的默认参数。
/* PBASE.H */
class pBase : public sf::Thread {
private:
bool Running;
public:
sf::Mutex Mutex;
WORD OriginalColor;
pBase(){
Launch();
Running = true;
OriginalColor = 0x7;
}
void progressBar(int , int);
bool key_pressed();
void setColor( int );
void setTitle( LPCWSTR );
bool test_connection(){
if(Running == false){
return 0;
}
else{
return 1;
}
return 0;
}
void Stop(){
Running = false;
if(Running == false) Wait();
}
};
/* PBASE.CPP */
// ... other stuff above
void pBase::setColor( int _color = -1){
if(_color == -1){
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ),FOREGROUND_INTENSITY | OriginalColor);
return;
}
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ),FOREGROUND_INTENSITY | _color);
}
And the error , taken from VC2010
和错误,取自 VC2010
Error 4 error C2572: 'pBase::setColor' : redefinition of default parameter : parameter 1
错误 4 错误 C2572:“pBase::setColor”:重新定义默认参数:参数 1
回答by Mahesh
You have to specify the default values for the arguments only in the declaration but not in the definition.
您必须仅在声明中而不是在定义中指定参数的默认值。
class pBase : public sf::Thread {
// ....
void setColor( int _color = -1 );
// ....
} ;
void pBase:: setColor( int _color )
{
// ....
}
The default value for an member function's argument can either go in declaration or definition but not both. Quote from ISO/IEC 14882:2003(E) 8.3.6
成员函数参数的默认值可以出现在声明或定义中,但不能同时出现。引自 ISO/IEC 14882:2003(E) 8.3.6
6) Except for member functions of class templates, the default arguments in a member function definition that appears outside of the class definition are added to the set of default arguments provided by the member function declaration in the class definition. Default arguments for a member function of a class template shall be specified on the initial declaration of the member function within the class template. [Example:
6) 除类模板的成员函数外,出现在类定义之外的成员函数定义中的默认参数被添加到类定义中成员函数声明提供的默认参数集中。类模板成员函数的默认参数应在类模板内成员函数的初始声明中指定。[例子:
class C {
void f(int i = 3);
void g(int i, int j = 99);
};
void C::f(int i = 3) // error: default argument already
{ } // specified in class scope
void C::g(int i = 88, int j) // in this translation unit,
{ } // C::g can be called with no argument
—end example]
—结束示例]
According to the standard provided example, it should actually work the way you did. Unless you have done like this, you shouldn't actually get the error. I amn't sure why it actually worked in your case with my solution. Probably something visual studio related, I guess.
根据标准提供的示例,它实际上应该按照您的方式工作。除非你做了这样,你不应该真正得到错误。我不确定为什么它在我的解决方案中确实适用于您的情况。我猜可能是视觉工作室相关的东西。
回答by MasterHD
Alright! It worked (a bit weird though because it was working fine when i had the whole code in one file).
好吧!它起作用了(虽然有点奇怪,因为当我将整个代码放在一个文件中时它运行良好)。
I also had this problem when I started moving code around into multiple files. The real problem was that I forgot to write
当我开始将代码移动到多个文件中时,我也遇到了这个问题。真正的问题是我忘记写了
#pragma once
at the top of the header file, and so it was redefining the functions multiple times (each time the header file was being invoked from a parent file), this caused the redefinition of default parametererror.
在头文件的顶部,因此多次重新定义函数(每次从父文件调用头文件时),这导致重新定义默认参数错误。