main.obj:错误 LNK2019:无法解析的外部符号 public:__cdecl

main.obj : error LNK2019: unresolved external symbol public: __cdecl

我正在尝试使用 Qt5 在 C++ 中编写一个简单的 class。我不知道这个错误来自:

main.obj : error LNK2019: unresolved external symbol "public: __cdecl ItemModel::ItemModel(class std::basic_string,class std::allocator >)" (??0ItemModel@@QEAA@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function main debug\AMWS.exe : fatal error LNK1120: 1 unresolved externals

Test.pro

QT += core
QT -= gui

CONFIG += c++11

TARGET = Test
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

HEADERS += \
    main.h \
    itemmodel.h

SOURCES += \
    main.cpp \
    itemmodel.cpp

itemmodel.h

#ifndef ITEMMODEL_H
#define ITEMMODEL_H

#include <string>

class ItemModel
{
public:
    ItemModel(std::string sku);
protected:
    std::string SKU;
};

#endif // ITEMMODEL_H

itemmodel.cpp

#include "itemmodel.h"

using namespace std;

ItemModel::ItemModel(string sku) : SKU(sku)
{

}

main.h

#ifndef MAIN_H
#define MAIN_H

#include <QCoreApplication>
#include <iostream>
#include "itemmodel.h"

#endif // MAIN_H

main.cpp

#include "main.h"
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    ItemModel product("dummy");

    cout << "Great!!!" << endl;

    return a.exec();
}

尝试删除完整的构建文件夹(包括 Makefile)并重新编译。

有时 Qt 应用程序会出现奇怪的链接器错误,因为 makefile 没有在应该更新的时候被 qmake 更新。