I am new to C++ and I am trying to use C++ source files in combination with the header files. I surfed through the internet and found this article from Microsoft on C++ header files. I did exactly the same thing but my code doesn't work. Can anyone please guide me what am I doing wrong.
This is the folder tree.
library
├── Class.cc
├── Class.hh
└── Main.cc
Here is my code:
// Class.cc
#include "Class.hh"
using namespace mynamespace;
int Class::get_field(void)
{
return -1;
}
// Class.hh
namespace mynamespace
{
class Class
{
public:
int get_field(void);
};
}
// Main.hh
#include <iostream>
#include "Class.hh"
using namespace std;
using namespace mynamespace;
int main(int argc, char const *argv[])
{
Class c = Class();
cout << c.get_field() << endl;
return 0;
}
This is the output:
/tmp/ccyKPmWv.o: In function `main':
Main.cc:(.text+0x26): undefined reference to `mynamespace::Class::get_field()'
collect2: error: ld returned 1 exit status
In the Main.cc
file when I replace #include "Class.hh"
with #include "Class.cc"
then it works as expected!