main.obj:错误LNK2019:未解析的外部符号public:__ cdecl

时间:2017-02-02 15:20:17

标签: c++ qt class qt5 linker-errors

我正在尝试使用Qt5在C ++中编写一个简单的类。我无法弄清楚这个错误来自:

  

main.obj:错误LNK2019:未解析的外部符号“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)在函数main中引用       debug \ AMWS.exe:致命错误LNK1120:1个未解析的外部

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();
}

1 个答案:

答案 0 :(得分:3)

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

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