在包括QtNetwork时,我遇到了很多错误

While including QtNetwork I get a lot of errors

本文关键字:错误 遇到 包括 QtNetwork      更新时间:2023-10-16

我使用带有内置Qt的VS2010。我正在尝试实现客户端和服务器,使用UDP。我以Qt4书中的GUI C++编程为例。

实现主要基于Qt库QtNetwork,但当我包含它时,我会遇到很多未知的错误,比如:

Error 8 error C2653: 'System' : is not a class or namespace name ****AssemblyInfo.cpp 3
Error 9 error C2871: 'Reflection' : a namespace with this name does not exist ****AssemblyInfo.cpp 3

我认为这些错误是库QtNetwork没有正确包含的结果。你能告诉我如何解决这个问题吗?!

我试图用我的方法解决它,并采取了以下行动:

  1. 在VS:Qt\Qt项目设置\添加网络库,然后添加qmake -projectqmakenmake

  2. 在.pro文件中添加了字符串QT += network,然后是qmake -projectqmakenmake

他们两人都没能处理好这个问题。

#pragma once
#include <QWidget>
#include "QtNetwork"
#include <QtGui>
class QTimer;
class QUdpSocket;
 class NetworkManagerServer : public QWidget
 {
     Q_OBJECT
 public:
     NetworkManagerServer(QWidget *parent = 0);
 private slots:
     void sendDatagramm();
 private:
     QUdpSocket* m_udpSocket;
     QTimer* m_timer;
     int m_messageNo;
 };
#include "NetworkManagerServer.h"
 NetworkManagerServer::NetworkManagerServer(QWidget *parent)
     : QWidget(parent)
 {
     m_timer = new QTimer(this);
     m_timer->start(2 * 1000);
     m_udpSocket = new QUdpSocket(this);
     m_messageNo = 1;
     connect(m_timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));
 }
 void NetworkManagerServer::sendDatagramm(void)
 {
     QByteArray datagramm;
     QDataStream out (&datagramm, QIODevice::WriteOnly);
     //out.setVersion(QDataStream::Qt_4_8);
     out << "Hellow Qt::Network!";
     m_udpSocket->writeDatagram(datagramm, QHostAddress::LocalHost,5824);
 }

以下是我得到的错误列表:

Error   8   error C2653: 'System' : is not a class or namespace name    ***AssemblyInfo.cpp    3
Error   9   error C2871: 'Reflection' : a namespace with this name does not exist   ***AssemblyInfo.cpp 3
Error   10  error C2653: 'System' : is not a class or namespace name    ***AssemblyInfo.cpp 4
Error   11  error C2871: 'CompilerServices' : a namespace with this name does not exist ***AssemblyInfo.cpp 4
Error   12  error C2653: 'System' : is not a class or namespace name    ***AssemblyInfo.cpp 5
Error   13  error C2871: 'InteropServices' : a namespace with this name does not exist  ***AssemblyInfo.cpp 5

依此类推

我发现这就是问题所在!!!1.我创建了单元测试,后来从项目中删除了它们,但没有t removed from folder as result lots of errors, which i presented here 2. I tried to include QtNetwork with < > , but it didnt作品3。但当我qmake-project、qmake、nmake和生成的.pro文件中包含字符串QT+=网络项目时,已经成功编译了!!!