未定义的类引用

时间:2013-08-30 05:05:31

标签: c++ qt

我知道这已被问了好几千次,但我很难过。在过去的3天里,我一直在寻找所有的结果。我一直收到这个错误,我无法弄清楚原因。 我只添加了我输入的代码/重要的代码。如果我注释掉我的代码,程序编译没有问题。我做错了什么???

  

CMakeFiles / brewtarget.dir / MainWindow.cpp.o:在函数MainWindow :: MainWindow(QWidget *)'中:   MainWindow.cpp :(。text + 0xb145):未定义引用yeastCellCounter :: yeastCellCounter()'

CODE

mainwindow.cpp

#include "yeastcellcounter.h"

// a whole lot of stuff between these...

yeastCountDialog = new yeastCellCounter();

mainwindow.h

class yeastCellCounter;

// A whole log of stuff between these...

yeastCellCounter *yeastCountDialog;

yeascellcounter.cpp

#include "yeastcellcounter.h"

yeastCellCounter::yeastCellCounter(){}

yeastcellcounter.h

#ifndef YEASTCELLCOUNTER_H
#define YEASTCELLCOUNTER_H

class yeastCellCounter
{
public:
    yeastCellCounter();
};

#endif // YEASTCELLCOUNTER_H

这是cmakelist.txt

中的INCLUDE_DIRECTORIES指令
SET(ROOTDIR "${CMAKE_CURRENT_SOURCE_DIR}")
SET(SRCDIR "${ROOTDIR}/src")
SET(UIDIR "${ROOTDIR}/ui")
SET(DATADIR "${ROOTDIR}/data")
SET(TRANSLATIONSDIR "${ROOTDIR}/translations")
SET(WINDIR "${ROOTDIR}/win")

INCLUDE_DIRECTORIES(${SRCDIR})
INCLUDE_DIRECTORIES("${CMAKE_BINARY_DIR}/src") # In case of out-of-source build.
INCLUDE_DIRECTORIES("${CMAKE_BINARY_DIR}/QtDesignerPlugins")

2 个答案:

答案 0 :(得分:2)

每当您看到undefined reference to ...类型的错误时,都会发生链接器错误。这意味着编译器已完成它的工作,并且所有目标文件都已编译而没有错误。现在是链接器将所有部分组合到一个文件中的时候了。

在您的具体示例中,它表示无法找到函数yeastCellCounter::yeastCellCounter()的定义。从您粘贴的代码中,此函数(尽管为空)在文件yeascellcounter.cpp中明确定义。

看起来你的cmakelists.txt文件不完整。您尚未指定需要将哪些源文件链接在一起以创建最终可执行文件。您需要使用add_executable语句。

这是simple example

答案 1 :(得分:1)

问题是:

yeastCountDialog = new yeastCellCounter();

应该是:

yeastCountDialog = new yeastCellCounter;

(注意缺少括号)。始终调用默认构造函数而不使用括号。而且,您需要将“yeastcellcounter.cpp”添加到cmake源列表中。