Note. I edited my old question as clear as possible.
I have a third-party library named person.lib and its title is person.h . This is my actual project structure, and it compiles and works just fine.
Actual structure:
main.cpp
#include <iostream> #include <time.h> #include <ctype.h> #include <string> #include "person.h" using namespace person; using namespace std; class Client : public Person { public: Client(); void onMessage(const char * const); private: void gen_random(char*, const int); }; Client::Client() { char str[11]; gen_random(str, 10); this->setName(str); } void Client::onMessage(const char * const message) throw(Exception &) { cout << message << endl; } void Client::gen_random(char *s, const int len) { //THIS FUNCTION GENERATES A RANDOM NAME WITH SPECIFIED LENGTH FOR THE CLIENT } int main() { try { Person *p = new Client; p->sayHello(); } catch(Exception &e) { cout << e.what() << endl; return 1; } return 0; }
Now I want to reorganize my code by splitting the declaration of my Client class into its definition and creating client.h and client.cpp . ATTENTION : sayHello() and onMessage(const * char const) are functions of the person library.
Reorganized structure:
main.cpp
#include <iostream> #include "client.h" using namespace person; using namespace std; int main() { try { Person *p = new Client; p->sayHello(); } catch(Exception &e) { cout << e.what() << endl; return 1; } return 0; }
client.cpp
#include "client.h" using namespace person; using namespace std; Client::Client() { char str[11]; gen_random(str, 10); this->setName(str); } void Client::onMessage(const char * const message) throw(Exception &) { cout << message << endl; } void Client::gen_random(char *s, const int len) {
client.h
#ifndef CLIENT_H #define CLIENT_H #include <time.h> #include <ctype.h> #include <string> #include "person.h" class Client : public Person { public: Client(); void onMessage(const char * const); private: void gen_random(char*, const int); }; #endif
As you can see, I just created client.h in which there is an inclusion of the base class person.h , then I created client.cpp in which there is an inclusion of client.h and definition of its functions. Now compilation gives me the following errors:
error C2504: 'Person': base class undefined client.h 7 1 Test error C2440: 'inizialization': unable to convert from 'Client *' to 'person::impl::Person *' main.cpp 15 1 Test error C2504: 'Person': base class undefined client.h 7 1 Test error C2039: 'setName': is not a member of 'Client' client.cpp 8 1 Test error C3861: 'sendMessage': identifier not found client.cpp 34 1 Test
It's just a cutter to copy and copy, but it doesn't work, and I really don't understand WHY! What is the solution and why does it give me these errors? Is there anything about the C ++ structure that I am missing? Thanks.
c ++ compilation header
Angelo
source share