Please remove the multiple-inclusion guards from the .cpp files. They are not necessary, nor should they ever be used other than in a header file.
Also, do not include a .cpp file within another .cpp file; that is crude programming.
Lastly, never specify a "using namespace" in a header file.
Here's a better approach:
Person.h:
Code:
#ifndef PERSON_H
#define PERSON_H
#include <string>
namespace company
{
struct Person
{
std::string name;
int age;
};
}
namespace company2
{
std::string Person2Str(const company::Person& p);
}
#endif
Person.cpp:
Code:
#include "Person.h"
#include <string>
namespace company2
{
std::string Person2Str(const company::Person& p)
{
return p.name;
}
}
Main.cpp:
Code:
#include "Person.h"
#include <string>
int main()
{
using namespace company;
Person p = { "Fred", 30 };
std::string name = company2::Person2Str(p);
...
}
To compile multiple modules:
Code:
g++ Main.cpp Person.cpp -o myprog
-----------
EDIT: Sorry for the post-edits; your code still confuses me. But hopefully what I have above will clarify things. I have not attempted to compile the code, so you may still have trouble.