The syntax for the constructor declaration in the header (.h) and then the definition in the C ++ class file (.cpp)

If anyone could state this, I would appreciate it. An example of what I thought would work (suppose the necessary #include instructions exist):

//.h file class someclass(){} //.cpp someclass:: someclass(){ //implementation // of //class }; 
+4
source share
3 answers

file someclass.h

 #ifndef SOME_CLASS_H #define SOME_CLASS_H class someclass { public: someclass(); // declare default constructor private: int member1; }; #endif 

someclass.cpp

 someclass::someclass() // define default constructor : member1(0) // initialize class member in member initializers list { //implementation } 
+11
source

Title:

 //.h file class someclass { someclass(); }; // <-- don't forget semicolon here 

A source:

 #include "someClass.h" //.cpp someclass::someclass() { // Implementation goes here } // <-- No semicolon here 
+2
source

You must declare a constructor in your class if you want to provide a definition for it. You do only the second thing.

In addition, the original class definition contains some errors: parentheses are not required after the class name, and a semicolon is required after the final brace.

 class someclass { someClass(); // Here you DECLARE your constructor }; ... someclass::someclass() // Here you DEFINE your constructor { ... } 
+1
source

All Articles