简体   繁体   English

为什么我不能在Qt中使用普通的C ++类

[英]why I can't use normal C++ classes with Qt

Can anyone pls tell me that, why I can't use normal C++ classes within a Qt programme. 谁能告诉我,为什么我不能在Qt程序中使用普通的C ++类。 If there is any class which aren't inherited from QObject the compiler give me a linking error called, 如果有任何类不是从QObject继承的,编译器会给我一个链接错误,

error LNK2019: unresolved external symbol _main referenced in function _WinMain@16

I'm using Qt 4.5.2 (compiled by myself) with vs2005. 我在vs2005上使用Qt 4.5.2(由我自己编译)。 Pls help me to solve this ! 请帮我解决这个问题!

Edit: 编辑:

Example... 例...

//UnitManager.h //UnitManager.h

class UnitManager
{
public:
//-Some code
};

//CivilizationViewer.h //CivilizationViewer.h

class CivilizationViewer : public QMainWindow
{
Q_OBJECT
//-some code
};

//main //主要

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    CivilizationViewer w;
    w.show();
    return a.exec();
}

If I include UnitManager.h in CivilizationViewer.h compiler will give me that error. 如果我在CivilizationViewer.h中包含UnitManager.h,则编译器会给我该错误。 (eventhough I include UnitManager.h in main.cpp compiler will give me the error) (尽管我在main.cpp编译器中包含UnitManager.h也会给我错误)

The error you gave doesn't have anything to do with what classes you're using. 您给出的错误与您正在使用的类无关。 It looks like it's related to the entry point you have set for your application. 看起来它与您为应用程序设置的入口点有关。 Usually you want to use main() instead of WinMain() in Qt programs. 通常,您想在Qt程序中使用main()而不是WinMain()。 Make sure your configuration is set up right. 确保您的配置设置正确。

You included a little bit of code in your question. 您在问题中包含了一些代码。 Is that everything? 这是全部吗? If so, you're missing a main function. 如果是这样,则您缺少主要功能。

Thanks for everyone. 谢谢大家 I found the error. 我发现了错误。 There is SDL.h in the UnitManager.h, so I have to add SDL.lib and SDLmain.lib (it is correct, right ?) then there is another definition for main in SDLmain.lib. 在UnitManager.h中有SDL.h,因此我必须添加SDL.lib和SDLmain.lib(正确,对吗?),然后在SDLmain.lib中有main的另一个定义。 So, there was a coflict between definitions of main. 因此,在main的定义之间存在冲突。 Therefore I added SDLmain.lib before adding qtmaind.lib. 因此,我在添加qtmaind.lib之前添加了SDLmain.lib。 Then the problem solved by only giving a warning called, 然后问题解决了,只给出了一个警告,

warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library

What is that warning ? 那是什么警告? I can just ignore the warning, but I like to get to know it ! 我可以忽略警告,但我想了解它! Thanks 谢谢

It seems like you need to link with qtmain.lib (qtmaind.lib for debug builds). 似乎您需要链接到qtmain.lib(用于调试版本的qtmaind.lib)。 That library provides a WinMain function which is needed if you declared /subsystem:windows. 该库提供了WinMain函数,如果您声明/ subsystem:windows,则需要该函数。

Source: http://lists.trolltech.com/qt-interest/2005-12/thread00170-0.html 资料来源: http : //lists.trolltech.com/qt-interest/2005-12/thread00170-0.html

看来您没有正确包含QT库...(实际的.lib文件)

I think you actually made a win32 app. 我认为您实际上制作了一个win32应用程序。 try replacing your main for: 尝试将您的main替​​换为:

int _tmain(int argc, _TCHAR* argv[]){
  your code
}

Seeing your error msg, i wouldn't guess Qt was your problem. 看到您的错误消息,我不会猜到Qt是您的问题。 Did you install the Visual Studio Qt integration? 您是否安装了Visual Studio Qt集成? Try it and make a new Qt project. 尝试并创建一个新的Qt项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM