separate header & source files are not compiling | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

separate header & source files are not compiling

Separate header & source files are not compiling; compiler is returning me with an error viz undefined reference to 'MyClass::MyClass()' undefined reference to 'MyClass::myPrint()' please help me out what am i doing wrong ??? :( MyClass.h #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass(); void myPrint(); ~MyClass(); protected: private: }; #endif // MYCLASS_H MyClass.cpp #include "MyClass.h" #include <iostream> using namespace std; MyClass::MyClass() { cout<<"Constructor"; } MyClass::~MyClass() { cout<<"Destructor"; } MYClass::myPrint() { cout<<"Hello"<<endl; } main.cpp #include <iostream> #include "MyClass.h" using namespace std; int main() { MyClass obj; obj.myPrint(); return 0; }

23rd Oct 2016, 10:17 AM
Digbose Hazarika
Digbose Hazarika - avatar
3 Answers
+ 3
you need to write myPrint() like this: void MyClass::myPrint() within the .cpp file I'm not sure if it's only a typo here but you also spelled it MYClass in the myPrint()
23rd Oct 2016, 4:07 PM
Zeke Williams
Zeke Williams - avatar
+ 1
No problem. Glad it's fixed
24th Oct 2016, 8:23 PM
Zeke Williams
Zeke Williams - avatar
0
hey bruh thanks for the reply i finally got what was wrong with my code apart from the typos there was nothing wrong i was compiling it in a wrong way lol like if you use g++ you need to type ==> g++ main.cpp MyClass.h MyClass.cpp not g++ main.cpp or if using an ide then it must be created in a project :)
24th Oct 2016, 8:11 AM
Digbose Hazarika
Digbose Hazarika - avatar