0

我知道这已经被问了数千次,但我很难过。我一直在寻找过去3天没有结果。我不断收到这个错误,我不知道为什么。我只添加了我输入的/重要的代码。如果我注释掉我的代码,程序编译没有问题。我究竟做错了什么???

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

CODE

主窗口.cpp

#include "yeastcellcounter.h"

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

yeastCountDialog = new yeastCellCounter();

主窗口.h

class yeastCellCounter;

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

yeastCellCounter *yeastCountDialog;

yeascellcounter.cpp

#include "yeastcellcounter.h"

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")
4

2 回答 2

2

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

在您的具体示例中,它说它找不到函数的定义yeastCellCounter::yeastCellCounter()。从您粘贴的代码中,该函数虽然为空,但在文件中已明确定义yeascellcounter.cpp

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

这是一个简单的例子

于 2013-08-30T08:50:33.293 回答
1

The problem is:

yeastCountDialog = new yeastCellCounter();

It should be:

yeastCountDialog = new yeastCellCounter;

(Notice the lack of parentheses). The default constructor is always called without parentheses. And also, you need to add "yeastcellcounter.cpp" to the list of cmake sources.

于 2013-09-01T19:37:49.550 回答