Can anyone help me understand this error? “definition of implicitly-declared ‘classA::classA()’”

前端 未结 4 952
有刺的猬
有刺的猬 2021-01-07 17:33

Heres the code:

#include 
#include 
using namespace std;

class classA
{                   
      protected:
                v         


        
相关标签:
4条回答
  • 2021-01-07 17:45

    You forgot to declare the constructor in the class definition. Declare it in public section of the class (if you want clients to create instance using it):

    class classA
    { 
          public: 
                  classA();    // you forgot this!       
          protected:
                    void setX(int a);
    
          private:
                  int p;
    };
    

    Now you can write its definition outside the class which you've already done.

    0 讨论(0)
  • 2021-01-07 17:48

    An empty constructor is provided by default: this is correct. But if you redefine it, it's not a default constructor any more. You have to declare and define it. If you only declare it (without the body), it's incorrect: you have to define it as well. If you define it without a declaration in the class, it's an error as well. You can though, "combine" declaration and definition by writing as follows:

    class classA
    {
        // ....
        public:
           classA() { p = 0;} 
    };
    

    or in this case even better:

    class classA
    {
        // ....
        public:
           classA():p(0) {} 
    };
    
    0 讨论(0)
  • 2021-01-07 17:57
    class classA
    {                   
          protected:
                    classA(); // you were missing an explicit declaration!
                    void setX(int a);
    
          private:
                  int p;
    };
    
    classA:: classA()
    { 
     p = 0;
    }
    
    0 讨论(0)
  • 2021-01-07 18:07

    classA has no member named classA() to implement.

    class classA
    {
        // ....
        public:
           classA() ; // Missing the declaration of the default constructor.
    };
    
    0 讨论(0)
提交回复
热议问题