其中一个 QImage 构造函数给了我一个链接器错误,但另一个构造函数没有?

One of the QImage constructors gives me a linker error, but another constructor doesn't?

本文关键字:构造函数 一个 另一个 错误 QImage 链接      更新时间:2023-10-16

在我的项目中,我使用QImage来保存生成的图片,但当我调用时

QImage image(width, height, QImage::Format_RGB32);

visualstudio编译器给了我链接器错误:

error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QImage::~QImage(void)" (__imp_??1QImage@@UAE@XZ) referenced in function "void __cdecl lightTracer(void)" (?lightTracer@@YAXXZ)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QImage::QImage(int,int,enum QImage::Format)" (__imp_??0QImage@@QAE@HHW4Format@0@@Z) referenced in function "void __cdecl lightTracer(void)" (?lightTracer@@YAXXZ)

但如果我用替换上面的代码

QImage image();

我没有得到任何链接器错误,并且编译得很好。

这里出了什么问题?:(

更新:为了确保Qt正常工作,我尝试制作一个QString:

 QString s("hello world");

这样做效果很好。

QImage image();

这不会声明对象,也不会调用默认构造函数。它声明了一个名为image的函数,该函数没有参数,并返回一个QImage

这将调用默认构造函数:

QImage image;

这可能也会给你一个链接器错误;请确保您链接的是在.

中定义的任何库QImage

我在这个链接上发现了这个问题的提示。

我通过在Qt项目设置->Qt模块中包含GUI库模块来解决这个问题。

相关文章: