构造函数中对默认构造函数的未定义引用

Ety*_*Ety 0 c++ constructor default-constructor qt-creator

我正在使用 Qt Creator 开发一个项目。

假设我有一个简单的工具类:

#ifndef TOOL_H
#define TOOL_H

#include <map>
#include <string>
#include "myobject.h"

class Tool
{
public:
    Tool();

private:
    const std::map<std::string, MyObject*> myMap;
};

#endif // TOOL_H
Run Code Online (Sandbox Code Playgroud)

使用这样的默认构造函数:

#include "tool.h"

using namespace std;

Tool::Tool()
{
    myMap = new map<string, MyObject*>();
    // populate myMap
}
Run Code Online (Sandbox Code Playgroud)

然后我想在另一个类的构造函数中使用该类:

#include "myclass.h"

using namespace std;

MyClass::MyClass()
{
    Tool t;
}
Run Code Online (Sandbox Code Playgroud)

但我收到以下编译错误:

In function 'MyClass::MyClass()':
undefined reference to 'Tool::Tool()'
Run Code Online (Sandbox Code Playgroud)

自然,myclass.h包含tool.h,所以我不明白为什么它找不到它。我尝试使用指针,将 t 声明为成员变量,但我不断收到此错误。

我尝试使用这些类在我的外部创建一个最小的项目,并使用 g++ 对其进行编译(工具,然后是带有主函数的 MyClass);有效。那么也许 Qt Creator 如何处理编译和链接有问题?但我不知道该检查哪个选项。

谢谢你的任何想法。

编辑:问题来自 Qt Creator 环境。我实际上在 Tool 之前创建了 MyClass,并且没有清理项目编译失败,并且没有告诉我错误是什么;真正的错误myMap = new map<string, MyObject*>();在 Tool 类中,因此 Tool 从未被编译(如 Jarod42 提到的),因此 MyClass 的编译错误。

清理和重建指出了真正的错误,并允许我修复我的项目。

Dut*_*tow 5

undefined reference to 'Tool::Tool()'是链接器错误消息。

Tool::Tool()这意味着您的 cpp 文件单独编译成功,但链接器在创建二进制文件时找不到。

这很可能是您的构建系统中的项目设置问题。