编译器抱怨我的默认参数?

发布于 2024-11-13 09:15:36 字数 1265 浏览 3 评论 0原文

我在处理这段代码时遇到了麻烦,在我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑脸一如从前 2024-11-20 09:15:36

您必须仅在声明中而不是在定义中指定参数的默认值。

 class pBase : public sf::Thread {
     // ....
     void setColor( int _color = -1 );
     // ....
 } ;

 void pBase:: setColor( int _color )
 {
     // ....
 }

成员函数参数的默认值可以出现在声明或定义中,但不能同时出现在两者中。引自 ISO/IEC 14882:2003(E) 8.3.6

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

—结束示例]

根据提供的标准示例,它实际上应该按照您的方式工作。除非您已这样做,否则您实际上不应该收到错误。我不确定为什么它在我的解决方案中实际上适用于您的情况。我猜可能是与视觉工作室相关的东西。

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

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:

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.

一杯敬自由 2024-11-20 09:15:36

好吧!它工作了(虽然有点奇怪,因为当我将整个代码放在一个文件中时它工作正常)。

当我开始将代码移动到多个文件中时,我也遇到了这个问题。真正的问题是我忘记

#pragma once

在头文件的顶部写入,因此它多次重新定义函数(每次从父文件调用头文件时),这导致默认值的重新定义参数错误。

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 parameter error.

梦回梦里 2024-11-20 09:15:36

就我而言,我在多个路径中有相同的头文件。通过删除多余文件即可解决该错误。

In my case, I have the same header file in multiple paths. The error is solved by removing redundant files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文