如何正确扩展C++中的类并编写其头文件?

Ang*_*elo 10 c++ compilation header

注意:我已经编辑了我的旧问题,尽可能清楚.

我有一个名为的第三方库person.lib及其标题person.h.这是我的实际项目结构,它编译和运行完美.

实际结构:

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;
}
Run Code Online (Sandbox Code Playgroud)

现在,我想通过将Client类的声明与其定义分开并创建client.h和来重构我的代码client.cpp.请注意:sayHello()并且onMessage(const * char const)人员库的功能.

重构结构:

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;
    }
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我只是简单地创建了一个client.h包含基类的东西person.h,然后我创建了它的函数client.cpp的包含client.h和定义.现在,编译给了我这些错误:

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
Run Code Online (Sandbox Code Playgroud)

这只是一个剪切和复制重构,但它不起作用,我真的不明白为什么!什么是解决方案以及为什么它会给我这些错误?我缺少关于C++结构的东西吗?谢谢.

enh*_*lep 7

这是一个dog-n-bird实现(ruff ruff,cheep cheep)cLawyer在main.cpp中定义和实现,而cPerson和cClient在他们自己的头文件中定义,在他们自己的cpp文件中实现.更好的方法是存储类的名称.然后,人们不需要重载speak方法 - 可以简单地在每个派生副本中设置className.但是,根据我的估计,这可能是一个不太有用的例子.

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;
}
Run Code Online (Sandbox Code Playgroud)

cPerson.h

#ifndef cPerson_h_
#define cPerson_h_

class cPerson
{
    public:
        cPerson();
        virtual ~cPerson();
        virtual void talk(char *sayWhat);
    protected:
    private:
};

#endif // cPerson_h_
Run Code Online (Sandbox Code Playgroud)

cPerson.cpp

#include "cPerson.h"
#include <cstdio>
cPerson::cPerson()
{
    //ctor
}

cPerson::~cPerson()
{
    //dtor
}

void cPerson::talk(char *sayWhat)
{
    printf("cPerson says: '%s'\n",sayWhat);
}
Run Code Online (Sandbox Code Playgroud)

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_
Run Code Online (Sandbox Code Playgroud)

cClient.cpp

#include "cClient.h"
#include <cstdio>

cClient::cClient()
{
    //ctor
}

cClient::~cClient()
{
    //dtor
}
Run Code Online (Sandbox Code Playgroud)

产量

cPerson says: 'Hello world!'
cClient says: 'Hello world!'
cLawyer says: 'Hello $$$'
Run Code Online (Sandbox Code Playgroud)

建议如上:

//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";
Run Code Online (Sandbox Code Playgroud)


Shi*_*nko 5

您正在两次声明类Client - 一次在.h文件中,一次在.cpp.您只需要在.h文件中声明它.
你也需要把using namespace person;.h文件.
如果类Person在namcespace中,请使用它person::Person来访问它.

client.cpp只能包含定义!

我认为对于链接器,client.h中定义的类Client和client.cpp中定义的类Client是不同的类,因此它找不到Client :: Client()的实现.我的目的是从client.cpp中删除类Client的声明,并只留下函数的定义:

// 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
}
Run Code Online (Sandbox Code Playgroud)

  • 在头文件中应避免使用`namespace`. (9认同)