Error: default argument given for parameter after previous specification

20,237

Solution 1

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

  }
}

Solution 2

You're not allowed to repeat default arguments when you define a function. They belong only on the declaration. (The actual rule isn't quite that simple, because a definition can also be a definition, but you get the idea...)

Solution 3

You dont include the default parameter in your function definition, the prototype is the only one you need to include the default value into.

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

  }
Share:
20,237
Admin
Author by

Admin

Updated on October 05, 2020

Comments

  • Admin
    Admin over 3 years

    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(){
    
      }
    }
    
  • Synxis
    Synxis over 11 years
    You've left a default argument: Bordered = true ;)
  • Admin
    Admin over 11 years
    Amazing amazing amazing, my only remaining problem is the virtual keyword on my destructor that the compiler isn't letting me get away with...
  • GuangshengZuo
    GuangshengZuo over 5 years
    according to my practice, we can only add default parameters when declaring function, not definition.
  • Synxis
    Synxis over 5 years
    @GuangshengZuo Actually you can add them to the definition, as stated in my answer...