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

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

Heres the code:

#include 
#include 
using namespace std;

class classA
{                   
      protected:
                v         


        
4条回答
  •  一个人的身影
    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) {} 
    };
    

提交回复
热议问题