Question

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

Heres the code:

#include <cstdlib>
#include <iostream>
using namespace std;

class classA
{                   
      protected:
                void setX(int a);

      private:
              int p;
};

classA:: classA()
{                      //error here.
 p = 0;
}

void classA:: setX(int a)
{
 p = a;    
}

int main()
{   
    system("PAUSE");
    return EXIT_SUCCESS;
}
Enquirer: darko

||

Solution #1:

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.

Respondent: Nawaz

Solution #2:

class classA
{                   
      protected:
                classA(); // you were missing an explicit declaration!
                void setX(int a);

      private:
              int p;
};

classA:: classA()
{ 
 p = 0;
}

Solution #3:

classA has no member named classA() to implement.

class classA
{
    // ....
    public:
       classA() ; // Missing the declaration of the default constructor.
};
Respondent: Mahesh

Solution #4:

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) {} 
};
Respondent: Mikhail Semenov

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy