本来不喜欢作伸手党的,但是这些错误是在是找不到原因.
网上找了找 也没啥答案.
应该都是些语法错误,请帮忙改正一下吧.
net.h
#ifndef NET_H
#define NET_H
#include <QTcpServer>
#include <iostream>
#include <QString>
#include <QTcpServer>
#include <QThread>
#include <QTcpSocket>
#include <QObject>
class cThreadPoll:public QThread
{
//Q_OBJECT
public:
cThreadPoll(cServer *oSerSck)
{
sck = oSerSck->nextPendingConnection();
this->start();
}
private:
QTcpSocket *sck;
void run()
{
while(1)
{
std::cout << "thread is running!!" << std::endl;
this->sleep(1);
}
}
};
class cServer:public QObject,QTcpServer
{
Q_OBJECT
public:
cServer(int port, int max_num)
{
setMaxPendingConnections(max_num);
if(!listen(QHostAddress::Any, port))
std::cout << "端口:"<< port << "开始监听失败" << std::endl;
else
std::cout << "开始监听"<< serverAddress().toString().toStdString()
<< " 端口:"<< port
<< "的TCP连接,最大可响应连接:" << max_num
<< std::endl;
this->connect(this, SIGNAL(newConnection()), this, SLOT(acpt_connection()));
}
~cServer(){}
private slots:
void acpt_connection()
{
new cThreadPoll(this);
}
};
#endif // NET_H
main.cpp
#include <QCoreApplication>
#include "net.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cServer aa(52302,100);
return a.exec();
}
------解决方案--------------------
先把类分成.h和.cpp两个文件。
------解决方案--------------------
#include cServer.h