编译器抱怨我的默认参数?
我在处理这段代码时遇到了麻烦,在我从 main.cpp 文件中取出此类并将其拆分为 .h 和 .cpp 后,编译器开始抱怨我在 void 中使用的默认参数。
/* 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);
}
以及错误,取自VC2010
错误 4 错误 C2572:'pBase::setColor':重新定义默认参数:参数 1
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.
/* 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
Error 4 error C2572: 'pBase::setColor' : redefinition of default parameter : parameter 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须仅在声明中而不是在定义中指定参数的默认值。
成员函数参数的默认值可以出现在声明或定义中,但不能同时出现在两者中。引自 ISO/IEC 14882:2003(E) 8.3.6
根据提供的标准示例,它实际上应该按照您的方式工作。除非您已这样做,否则您实际上不应该收到错误。我不确定为什么它在我的解决方案中实际上适用于您的情况。我猜可能是与视觉工作室相关的东西。
You have to specify the default values for the arguments only in the declaration but not in the definition.
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
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.
当我开始将代码移动到多个文件中时,我也遇到了这个问题。真正的问题是我忘记
在头文件的顶部写入,因此它多次重新定义函数(每次从父文件调用头文件时),这导致默认值的重新定义参数错误。
I also had this problem when I started moving code around into multiple files. The real problem was that I forgot to write
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 parameter error.
就我而言,我在多个路径中有相同的头文件。通过删除多余文件即可解决该错误。
In my case, I have the same header file in multiple paths. The error is solved by removing redundant files.