C++构造函数继承无法正常工作

发布于 2024-12-17 21:18:02 字数 2068 浏览 0 评论 0原文

这是依赖的类头

#ifndef TransHeader
#define TransHeader
#include <string>
#include "CipherHeader.h"

using namespace std;
class Trans : public Cipher {
    public:
    Trans(string filenameIn);
    const static int MAXSIZE = 10000;
    void createArray();
    void transEncrypt();
    void transDecrypt();


    private:
        //string Key;
        //string inputData;
        char dataArray[MAXSIZE][MAXSIZE];


};

#endif

这是继承的头

#ifndef CipherHeader
#define CipherHeader
#include <string>
using namespace std;

class Cipher {
    public:
        const static int MAXSIZE = 10000;
        Cipher(string filenameIn);
        void getKey();
        void flagHandle(string);
        string Key;
        string inputData;
        string filename;
        string flags;

        string readFile();
        void writeFile();
    private:




};

#endif

问题是在我调用基本构造函数之后,

Trans::Trans(string filenameIn) : Cipher(filenameIn) {}

我无法像这样在普通文件中调用构造函数:

#include "Trans.cpp"

int main() {
string a  = "asdf";
Trans *c = new Trans(a);

}

这会导致此错误:

g++ test.cpp -o test.out
/tmp/ccbuqMYr.o: In function `Trans::Trans(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
test.cpp:(.text+0x35): undefined reference to `Cipher::Cipher(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccbuqMYr.o: In function `Trans::Trans(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
test.cpp:(.text+0xa5): undefined reference to `Cipher::Cipher(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

用 Cipher 替换 Trans 工作正常并运行。我已经尝试了我所知道的一切,过度谷歌搜索,但无法找出这个错误。其他设计问题等等稍后再处理,这是我的主要问题。请帮忙。

编辑:: 密码的定义

Cipher::Cipher(string filenameIn) {

filename = filenameIn;

readFile();
getKey();

}

This is the dependent class header

#ifndef TransHeader
#define TransHeader
#include <string>
#include "CipherHeader.h"

using namespace std;
class Trans : public Cipher {
    public:
    Trans(string filenameIn);
    const static int MAXSIZE = 10000;
    void createArray();
    void transEncrypt();
    void transDecrypt();


    private:
        //string Key;
        //string inputData;
        char dataArray[MAXSIZE][MAXSIZE];


};

#endif

This is the inherited header

#ifndef CipherHeader
#define CipherHeader
#include <string>
using namespace std;

class Cipher {
    public:
        const static int MAXSIZE = 10000;
        Cipher(string filenameIn);
        void getKey();
        void flagHandle(string);
        string Key;
        string inputData;
        string filename;
        string flags;

        string readFile();
        void writeFile();
    private:




};

#endif

The problem is after I call the base constructor

Trans::Trans(string filenameIn) : Cipher(filenameIn) {}

I cannot call the constructor in a normal file like so:

#include "Trans.cpp"

int main() {
string a  = "asdf";
Trans *c = new Trans(a);

}

This results in this error:

g++ test.cpp -o test.out
/tmp/ccbuqMYr.o: In function `Trans::Trans(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
test.cpp:(.text+0x35): undefined reference to `Cipher::Cipher(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccbuqMYr.o: In function `Trans::Trans(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
test.cpp:(.text+0xa5): undefined reference to `Cipher::Cipher(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status

replacing Trans with Cipher works just fine and runs. I have tried everything I know, googled excessively, and cannot figure out this error. Other design problems, ect will be dealt with later, this is my main problem. Please help.

EDIT:: definition of Cipher

Cipher::Cipher(string filenameIn) {

filename = filenameIn;

readFile();
getKey();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-12-24 21:18:02

好的规则 1 - 永远不要在头文件中放入“using”语句! ;-)

好的,您需要调用基类构造函数。

所以 :

Trans::Trans(const std::string &f) // notice pass by reference
    :Cipher(f)
{
}

Ok rule 1 - never ever ever put a 'using' statement in a header file! ;-)

Ok you need to call the base class constructor.

So :

Trans::Trans(const std::string &f) // notice pass by reference
    :Cipher(f)
{
}
江心雾 2024-12-24 21:18:02

您需要构建中的所有源文件:

g++ test.cpp trans.cpp cipher.cpp -o test

这会给您带来新的错误,因为您包含了 test.cpp 中的 trans.cpp 源文件,而不是头文件 - 修复这个问题,它应该不会再出现问题了。

You need all the source files in the build:

g++ test.cpp trans.cpp cipher.cpp -o test

This will give you a new error because you're including the trans.cpp source file from test.cpp, rather than the header file - fix that, and it should build with no further problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文