I am not able to understand that why this code gives linker error. I have a project with these two files
myclass.cpp
class MyClass
{
public:
void SomeFun(){ ... } // SomeFun is defined here
};
main.cpp
class MyClass
{
public:
void SomeFun(); // SomeFun is declared here
};
int main()
{
MyClass obj;
obj.SomeFun(); // This throws undefined reference linker error during build. Why?
}
1: In Summary, I have a class "MyClass" with function "SomeFun" declared in main.cpp and defined in myclass.cpp. I expected the build to succeed since during linking stage it should have found the definition of SomeFun(). But it fails !
2: But why does it work fine if I just move the class declaration in lets say file "myclass.h" and include that header in both cpp ( with function body defined in myclass.cpp). How header file is making difference?