C++ compiler error: return type specification for constructor invalid

C++ compiler error: return type specification for constructor invalid

C++ compiler error: return type specification for constructor invalid

A constructor has no return type:

class Molecule
{
 public:
  Molecule();  // constructor. No return type.
  bool read();
  void display() const;
};

Molecule::Molecule(){
       int count;
       count = 0;
}//end constructor

Also note that count is local to the body of the constructor, and you are not using it for anything.

Youre writing a constructor with a return type. Constructors have no return type. Just change your constructor definition into:

/* void */ Molecule::Molecule()
// ^^^^ Remove this
{
    int count;
    count = 0;
}

C++ compiler error: return type specification for constructor invalid

Constructor can not have return type.

update:

inline void Molecule::Molecule(){
       ^^^
       int count;
       count = 0;
}//end function

to:

Molecule::Molecule(){
       int count;
       count = 0;
}//end function

Related posts on c++ :

Leave a Reply

Your email address will not be published.