C++构造函数继承无法正常工作
这是依赖的类头
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的规则 1 - 永远不要在头文件中放入“using”语句! ;-)
好的,您需要调用基类构造函数。
所以 :
Ok rule 1 - never ever ever put a 'using' statement in a header file! ;-)
Ok you need to call the base class constructor.
So :
您需要构建中的所有源文件:
这会给您带来新的错误,因为您包含了
test.cpp
中的trans.cpp
源文件,而不是头文件 - 修复这个问题,它应该不会再出现问题了。You need all the source files in the build:
This will give you a new error because you're including the
trans.cpp
source file fromtest.cpp
, rather than the header file - fix that, and it should build with no further problems.