对基础构造函数的未定义引用

时间:2012-12-31 08:31:56

标签: c++ inheritance undefined-reference

  

可能重复:
  “undefined reference to” in G++ Cpp

我有这个标题

#ifndef TEST_H_
#define TEST_H_

class TEST
{
public :
    TEST();
};


#endif /* TEST_H_ */

和此标题

#ifndef TESTS_H_
#define TESTS_H_
#include "TEST.h"

class TESTS : public TEST
{
public :
    TESTS();
};


#endif /* TESTS_H_ */

我实现了这样的标题:

#include <iostream>
using namespace std;
#include "TEST.h"
TEST:: TEST()
{
}
int main(int argc, char **argv) {
    return 0;
}

和此:

#include "TESTS.h"

TESTS :: TESTS() : TEST()
{
}


int main(int argc, char **argv) {
    return 0;
}

我得到了以下错误:

/tmp/cc4jN1HN.o:在函数TESTS::TESTS()': TESTS.cpp:(.text+0xd): undefined reference to TEST :: TEST()'

为什么?

我做错了什么?

3 个答案:

答案 0 :(得分:6)

这是链接器错误。您可能没有编译和链接所有必需的文件。它应该是例如:

$ g++ -Wall TEST.cpp TESTS.cpp -o TEST

你还需要摆脱一个main()函数。

答案 1 :(得分:1)

如果这应该是一个程序,它只能有一个main功能。如果这应该是两个程序,那么其中TESTS::TESTS的程序也需要TEST的构造函数。

答案 2 :(得分:0)

假设你的两个.cpp文件名为TEST.cpp和TESTS.cpp,我猜你只是在编译第一个文件,比如

g++ TESTS.cpp

但你需要将它们一起编译,例如用

g++ TEST.cpp TESTS.cpp

否则,TESTS.cpp在编译时将无法找到TEST.cpp中定义的代码。

相关问题