【问题标题】:Error: default argument given for parameter after previous specification错误:在先前规范之后为参数提供了默认参数
【发布时间】:2012-11-08 18:37:15
【问题描述】:

这里对我来说非常简单的任务,我不知道为什么这会给我带来问题,我只是让两个模型类尝试在它们的方法中没有任何逻辑的情况下使用已经给我的标题和声明进行编译。老实说,这只是一个剪切和粘贴的工作,但我仍然遇到了这个金块般的爱情 -

cbutton.cpp:11:44: error: default argument given for parameter 4 of ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.h:7:5: error: after previous specification in ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.cpp:11:44: error: default argument given for parameter 5 of ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.h:7:5: error: after previous specification in ‘cio::CButton::CButton(const char*, int, int, bool, const char*)’ [-fpermissive]
cbutton.cpp:19:41: error: default argument given for parameter 1 of ‘void cio::CButton::draw(int)’ [-fpermissive]
cbutton.h:11:10: error: after previous specification in ‘virtual void cio::CButton::draw(int)’ [-fpermissive]
cbutton.cpp:53:29: error: ‘virtual’ outside class declaration

这是我正在处理的文件。谢谢大家,一如既往!

#include "cfield.h"

namespace cio{
  class  CButton: public CField{

  public:
    CButton(const char *Str, int Row, int Col, 
            bool Bordered = true,
            const char* Border=C_BORDER_CHARS);
    virtual ~CButton();
    void draw(int rn=C_FULL_FRAME);
    int edit();
    bool editable()const;
    void set(const void* str);
  };
}    




#include "cbutton.h"

namespace cio {  

  CButton::CButton(const char *Str, int Row, int Col, 
          bool Bordered = true,
          const char* Border=C_BORDER_CHARS){

  }

  void CButton::draw(int rn=C_FULL_FRAME){

  }

  int CButton::edit(){

    return 0;
  }

  bool CButton::editable()const {

  return false;
  }

  void CButton::set(const void* str){

  }

  virtual CButton::~CButton(){

  }
}

【问题讨论】:

    标签: c++ default-arguments


    【解决方案1】:

    您在函数的定义中指定了一个默认参数,而它们在类声明中已经有一个默认参数。 您可以在类声明或函数定义中声明默认参数,但不能同时声明。

    编辑:错过了错误的结尾:error: ‘virtual’ outside class declaration。这是一个相当明显的编译器错误:virtual 关键字属于类声明,而不是函数定义。只需将其从析构函数的定义中删除即可。

    更正来源:

    namespace cio {  
    
      CButton::CButton(const char *Str, int Row, int Col, 
              bool Bordered, // No default parameter here,
              const char* Border){ // here,
    
      }
    
      void CButton::draw(int rn){ // and here
    
      }
    
      CButton::~CButton(){ // No virtual keyword here
    
      }
    }
    

    【讨论】:

    • 惊人的惊人,我唯一剩下的问题是我的析构函数上的 virtual 关键字,编译器不让我逃脱......
    • 根据我的实践,我们在声明函数时只能添加默认参数,不能定义。
    • @GuangshengZuo 其实you can add them to the definition,正如我的回答中所说...
    【解决方案2】:

    定义函数时不允许重复默认参数。它们只属于声明。 (实际的规则并不那么简单,因为定义也可以是定义,但你明白了......)

    【讨论】:

      【解决方案3】:

      你没有在你的函数定义中包含默认参数,原型是唯一需要包含默认值的原型。

      #include "cbutton.h"
      
      namespace cio {  
      
        CButton::CButton(const char *Str, int Row, int Col, 
                bool Bordered,
                const char* Border){ //remove in def
      
        }
      
        void CButton::draw(int rn){
      
        }
      

      【讨论】:

      • 你留下了一个默认参数:Bordered = true ;)
      猜你喜欢
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 2012-07-25
      • 1970-01-01
      相关资源
      最近更新 更多