I am trying to link a header file which has a class defined in it and a .cpp file that has the actual functions of that class to my main cpp file but I'm getting this error:
c++ DataMembers.cpp -o DataMembers Undefined symbols for architecture x86_64: "Cat::eat()", referenced from: _main in DataMembers-053507.o "Cat::meow()", referenced from: _main in DataMembers-053507.o "Cat::sleep()", referenced from: _main in DataMembers-053507.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [DataMembers] Error 1
Please note that all files exists in the same directory!
my main.cpp code is:
#include "Cat.h"
#include <iostream>
using namespace std;
int main()
{
Cat myCat;
myCat.eat();
myCat.meow();
myCat.sleep();
}
my Cat.h code:
#ifndef CAT_H_
#define CAT_H_
class Cat
{
public:
void meow();
void sleep();
void eat();
};
#endif
and Cat.cpp code is:
#include <iostream>
#include "Cat.h"
using namespace std;
void Cat::meow()
{
cout << "Meowwwwww!" << endl;
return;
}
void Cat::sleep()
{
cout << "Zzz ... " << endl;
return;
}
void Cat::eat()
{
cout << "Num Nom Nom .. Yummy" << endl;
return;
}
Interesting thing is when I change the header file in my main.cpp from #inclue "Cat.h"
into #include "Cat.cpp" the program compiles without any issues! I just don't know why?
I have searched for a solution but couldn't find any yet! and I need to be able to use header files I create myself!
Thank you in advance friends!