Error: default argument given for parameter after previous specification

user1438611 picture user1438611 · Nov 8, 2012 · Viewed 18.7k times · Source

very simple task for me here and I'm not sure why this is giving me problems, I'm simply making two mockup classes try to compile without any logic in their methods whatsoever using headers and declarations already given to me. Honestly this is just a cut and paste job more than anything, and yet I still came across this golden nugget of love -

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

Here are the files I'm working with. Thank you everyone, as always!

#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(){

  }
}

Answer

Synxis picture Synxis · Nov 8, 2012

You specified a default argument in the definition of the function, while they already had a default argument in the class declaration. You can declare default arguments in the class declaration or in the function definition, but not both.

EDIT: Missed the end of your errors: error: ‘virtual’ outside class declaration. It's a rather clear compiler error: virtual keywords belongs to class declarations, not function definitions. Simply remove it from the definition of your destructor.

Corrected source:

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

  }
}