0

separate files for classes

I am trying to write code with separate files for classes right now i have 3 files: ----------------------------------------------------------- Birthday.h ----------------------------------------------------------- #ifndef BIRTHDAY_H #define BIRTHDAY_H class Birthday { public: Birthday(int d, int m, int y); void printdate(); protected: private: int day; int month; int year; }; #endif // BIRTHDAY_H ----------------------------------------------------------- Birthday.cpp ----------------------------------------------------------- #include "Birthday.h" Birthday::Birthday(int d, int m, int y) : day(d), month(m), year(y) { //ctor } void Birthday::printdate() { cout<<<<day<<"/"<<month<<"/"<<year<<endl; } ----------------------------------------------------------- main.cpp ----------------------------------------------------------- #include <iostream> #include "Birthday.h" using namespace std; int main() { Birthday bd(7, 2, 1992); bd.printdate(); } ----------------------------------------------------------- when i build i get the error: C:\Users\nisha\AppData\Local\Temp\ccCJXkgP.o:main.cpp:(.text+0x26): undefined reference to `Birthday::Birthday(int, int, int)' C:\Users\nisha\AppData\Local\Temp\ccCJXkgP.o:main.cpp:(.text+0x32): undefined reference to `Birthday::printdate()' collect2.exe: error: ld returned 1 exit status please help!

25th Apr 2020, 1:09 PM
nishant nair
nishant nair - avatar
1 Odpowiedź
0
Hi I fixed it, the problem was with the way i was compiling the code. i was doing this: ----------------------------------------------------------- g++ main.cpp -o test.exe ----------------------------------------------------------- instead i had to do this: ----------------------------------------------------------- g++ -c Birthday.cpp g++ -c Person.cpp g++ -c main.cpp g++ main.o Birthday.o Person.o -o test.exe ----------------------------------------------------------- OR! i could create a make file like this ----------------------------------------------------------- Makefile ----------------------------------------------------------- # Makefile # Specify what I need in the end. One single executable outputFile : main.o Birthday.o Person.o # Read this as divisionExecutable depends on main.o div.o # But how is it produced??? Hmm...using the below statement g++ main.o Birthday.o Person.o -o outputFile # starts with tab, I repeat tab # But main.o is not there? So specify how it is produced. main.o : main.cpp Birthday.h Person.h g++ -c main.cpp ----------------------------------------------------------- the use in cmd ----------------------------------------------------------- Mingw32-make test.exe -----------------------------------------------------------
25th Apr 2020, 3:31 PM
nishant nair
nishant nair - avatar