How to extend a class in C ++ and write its header file?

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) { //THIS FUNCTION GENERATES A RANDOM NAME WITH SPECIFIED LENGTH FOR THE CLIENT } 

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.

+8
c ++ compilation header
source share
2 answers

Here the implementation of the canine bird (ruff ruff, cheep cheep) cLawyer is defined and implemented in main.cpp, while cPerson and cClient are defined in their own header files implemented in their own cpp file. The best approach is to keep the class name. Then there would be no need to overload the conversation method - it would just be possible to set the class name in each derived copy. But that would provide in my estimates, a less useful example for you.

main.cpp

 #include <cstdio> #include "cClient.h" class cLawyer : public cPerson { public: cLawyer() : cPerson() {} ~cLawyer() {} void talk(char *sayWhat){printf("cLawyer says: '%s'\n", sayWhat);} }; int main() { cPerson newPerson; cClient newClient; cLawyer newLawyer; newPerson.talk("Hello world!"); newClient.talk("Hello world!"); newLawyer.talk("Hello $$$"); return 0; } 

cPerson.h

 #ifndef cPerson_h_ #define cPerson_h_ class cPerson { public: cPerson(); virtual ~cPerson(); virtual void talk(char *sayWhat); protected: private: }; #endif // cPerson_h_ 

cPerson.cpp

 #include "cPerson.h" #include <cstdio> cPerson::cPerson() { //ctor } cPerson::~cPerson() { //dtor } void cPerson::talk(char *sayWhat) { printf("cPerson says: '%s'\n",sayWhat); } 

cClient.h

 #ifndef cClient_h_ #define cClient_h_ #include "cPerson.h" class cClient : public cPerson { public: cClient(); virtual ~cClient(); void talk(char *sayWhat); protected: private: }; #endif // cClient_h_ 

cClient.cpp

 #include "cClient.h" #include <cstdio> cClient::cClient() { //ctor } cClient::~cClient() { //dtor } 

Exit

 cPerson says: 'Hello world!' cClient says: 'Hello world!' cLawyer says: 'Hello $$$' 

Suggestions noted above:

 //In the cPerson class, a var char *m_className; //In the cPerson::cPerson constructer, set the var m_className = "cPerson"; //Re-jig the cPerson::speak method void cPerson::speak(char *sayWhat) { printf("%s says: '%s'\n", m_className, sayWhat); } // EDIT: *** remove the speak methods from the cClient and cLawyer classes *** //Initialize the clas name apporpriately in derived classes //cClient::cClient m_className = "cClient"; //Initialize the clas name apporpriately in derived classes //cLaywer::cLaywer m_className = "cLawyer"; 
+6
source share

You declare the class client twice - once in the .h file and once in .cpp . You only need to declare it in the .h file.
You also need to put using namespace person; to the .h file.
If the Person class is in namcespace, use person::Person to access it.

client.cpp should contain only definitions!

I think that for the linker, the Client class defined in client.h and the Client class defined in client.cpp are different classes, so it cannot find the implementation of Client :: Client (). I want to remove the Client class declaration from client.cpp and leave only function definitions there:

 // client.cpp #include <time.h> #include <ctype.h> #include <string> #include "client.h" using namespace std; Client::Client() { //DO STUFF } void Client::onMessage(const char * const message) { //DO STUFF } void Client::gen_random(char *s, const int len) { //DO STUFF } 
+5
source share

All Articles