我有一个名为person.lib的第三方库及其 header 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;
}
我想通过将Client类的声明与定义分开来重构代码,并创建client.hclient.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;
    }
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
如您所见,我只创建了一个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
这仅仅是剪切和复制重构,但是它不起作用,我真的不明白为什么!解决方案是什么,为什么会给我这些错误?我缺少有关C++结构的信息吗?

最佳答案

这是一个“狗与鸟”的实现(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;
}

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
}

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

上面提到的建议:
//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";

关于c++ - 如何正确扩展C++中的类并编写其头文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19079534/

10-09 06:36
查看更多