c++ - 在另一个解决方案 c++ 中创建对象时出现 LNK2019 错误

标签 c++ linker-errors lnk2019

我正在尝试使用另一个 C++ 解决方案的文件来创建对象。

  • 当我创建一个 MyClass1 对象时,程序出现以下错误:

    1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall MyClass1::~MyClass1(void)" (??1MyClass1@@UAE@XZ) referenced in function _main
    1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MyClass1::MyClass1(class QWidget *,class QFlags<enum Qt::WindowType>)" (??0MyClass1@@QAE@PAVQWidget@@V?$QFlags@W4WindowType@Qt@@@@@Z) referenced in function _main
    
  • 当我创建一个 MyClass2 对象时,程序会创建该对象。没有错误。

我应该怎么做才能正确创建 MyClass1 对象?


我删除了相关来源中所有不必要的部分。代码如下。

测试文件ma​​in.cpp

#include "tester.h"
#include <QtGui/QApplication>

#include "myclasses.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    tester w;
    w.show();

    MyClass1 aa;
    MyClass2 bbb;

    return a.exec();
}

myclasses.h 文件

#ifndef MYCLASSES_H
#define MYCLASSES_H

#include <QtGui/QMainWindow>
#include "ui_myclasses.h"

class MyClass1 : public QMainWindow
{
    Q_OBJECT
public:
    MyClass1(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~MyClass1();

    void setthemall(int number);

private:
    Ui::MyClass1Class ui;
    int totalnumber;
};

class MyClass2 
{
    int param1;
    void getit ();
};


#endif 

myclasses.cpp 文件

#include "myclasses.h"

MyClass1::MyClass1(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
    ui.setupUi(this);
}

MyClass1::~MyClass1()
{
}

void MyClass1::setthemall(int pnumber)
{
    totalnumber=pnumber;
}


void MyClass2::getit() {

};

最佳答案

似乎缺少 myclasses.o 文件。您确定要编译和链接 myclasses.o 吗?

因为这是 Qt,在将这个源文件添加到您的项目后,您是否运行了“qmake -project”?

关于c++ - 在另一个解决方案 c++ 中创建对象时出现 LNK2019 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4913630/

相关文章:

c++ - elf 文件中的 ".debug_info"部分是什么?

iphone - 链接器命令失败,PubNub 库在 swift 和静态框架中

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 链接某些 Windows API 时出现链接器错误

c++ - LNK2019 和 LNK1120 具有外部文件中的功能

c++ - 在 DirectDrawCreate 上解析 'LNK2019 unresolved external symbol'

visual-studio-2012 - 如何修复 Visual Studio 2012 错误 LNK2019:无法解析的外部符号“__declspec(dllimport) public: class std::basic_string?

c++ - 将位分配给动态位集以在 C++ 中运行

带有 omniORB 库的 PHP 扩展 CORBA 客户端给出运行时错误 "undefined symbol "

c++ - 检查传递给 cpp 中的 popen API 的命令中的错误