当前位置: 技术问答>linux和unix
qt和socket混合编程时,调用close,遇到的难题
来源: 互联网 发布时间:2017-01-13
本文导语: 1、请问,我在槽函数中调用socket的close(socket),会提示close与widget类中的close参数不匹配,这个问题如何解决? client_dlg.cpp: In member function `void ClientDialog::slotConnect()': client_dlg.cpp:55: no matching function for call to `Clien...
1、请问,我在槽函数中调用socket的close(socket),会提示close与widget类中的close参数不匹配,这个问题如何解决?
client_dlg.cpp: In member function `void ClientDialog::slotConnect()':
client_dlg.cpp:55: no matching function for call to `ClientDialog::close(int&)'
/usr/local/Trolltech/Qt-4.5.3/include/QtGui/qwidget.h:485: candidates are: bool
QWidget::close()
client_dlg.cpp:56:2: warning: no newline at end of file
make: *** [client_dlg.o] Error 1
2、调用socket的connect也提示也窗体类的connect参数不匹配,我在connect前加了::connect可以解决。(代码中47行)
但close前加::,编译提示close函数没有声明。
client_dlg.cpp: In member function `void ClientDialog::slotConnect()':
client_dlg.cpp:55: `::close' undeclared (first use here)
client_dlg.cpp:56:2: warning: no newline at end of file
make: *** [client_dlg.o] Error 1
有人遇到过这个问题吗?或知道如何解决?谢谢。
3、我用的是arm920t-eabi-4.1.2,Qt-4.5.3,就是我们视频和FTP中提供的。源代码如下:
头文件:client_dlg.h
#ifndef CLIENT_DLG_H
#define CLIENT_DLG_H
#include
class ClientDialog : public QDialog
{
Q_OBJECT
public:
ClientDialog( QWidget *parent=0, Qt::WindowFlags f=0 );
~ClientDialog();
public:
QPushButton *m_pConnectPushButton;
private slots:
void slotConnect();
};
#endif
实现文件:client_dlg.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define MAXDATASIZE 100 /*每次最大数据传输量 */
#include "client_dlg.h"
ClientDialog::ClientDialog( QWidget *parent, Qt::WindowFlags f )
: QDialog( parent, f )
{
setWindowTitle(tr("Client Dialog"));
m_pConnectPushButton = new QPushButton();
m_pConnectPushButton->setText(tr("Connect"));
connect( m_pConnectPushButton, SIGNAL( clicked() ), this, SLOT( slotConnect() ) );
}
ClientDialog::~ClientDialog()
{
delete m_pConnectPushButton;
m_pConnectPushButton = NULL;
}
void ClientDialog::slotConnect()
{
int sockfd, recvbytes;
char buf[MAXDATASIZE] = {0};
struct hostent *host;
struct sockaddr_in serv_addr;
if ((host=gethostbyname("192.168.0.66"))==NULL)
{ perror("gethostbyname出错!"); exit(1); }
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ perror("socket创建出错!"); exit(1); }
//初始化客户端
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
//connect
if (::connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1)
{ perror("connect error!"); exit(1); }
//recv
if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1)
{ perror("recv出错!"); exit(1); }
buf[recvbytes] = '';
printf("Received: %s",buf);
::close(sockfd);
}
谢谢!
client_dlg.cpp: In member function `void ClientDialog::slotConnect()':
client_dlg.cpp:55: no matching function for call to `ClientDialog::close(int&)'
/usr/local/Trolltech/Qt-4.5.3/include/QtGui/qwidget.h:485: candidates are: bool
QWidget::close()
client_dlg.cpp:56:2: warning: no newline at end of file
make: *** [client_dlg.o] Error 1
2、调用socket的connect也提示也窗体类的connect参数不匹配,我在connect前加了::connect可以解决。(代码中47行)
但close前加::,编译提示close函数没有声明。
client_dlg.cpp: In member function `void ClientDialog::slotConnect()':
client_dlg.cpp:55: `::close' undeclared (first use here)
client_dlg.cpp:56:2: warning: no newline at end of file
make: *** [client_dlg.o] Error 1
有人遇到过这个问题吗?或知道如何解决?谢谢。
3、我用的是arm920t-eabi-4.1.2,Qt-4.5.3,就是我们视频和FTP中提供的。源代码如下:
头文件:client_dlg.h
#ifndef CLIENT_DLG_H
#define CLIENT_DLG_H
#include
class ClientDialog : public QDialog
{
Q_OBJECT
public:
ClientDialog( QWidget *parent=0, Qt::WindowFlags f=0 );
~ClientDialog();
public:
QPushButton *m_pConnectPushButton;
private slots:
void slotConnect();
};
#endif
实现文件:client_dlg.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define MAXDATASIZE 100 /*每次最大数据传输量 */
#include "client_dlg.h"
ClientDialog::ClientDialog( QWidget *parent, Qt::WindowFlags f )
: QDialog( parent, f )
{
setWindowTitle(tr("Client Dialog"));
m_pConnectPushButton = new QPushButton();
m_pConnectPushButton->setText(tr("Connect"));
connect( m_pConnectPushButton, SIGNAL( clicked() ), this, SLOT( slotConnect() ) );
}
ClientDialog::~ClientDialog()
{
delete m_pConnectPushButton;
m_pConnectPushButton = NULL;
}
void ClientDialog::slotConnect()
{
int sockfd, recvbytes;
char buf[MAXDATASIZE] = {0};
struct hostent *host;
struct sockaddr_in serv_addr;
if ((host=gethostbyname("192.168.0.66"))==NULL)
{ perror("gethostbyname出错!"); exit(1); }
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{ perror("socket创建出错!"); exit(1); }
//初始化客户端
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
//connect
if (::connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1)
{ perror("connect error!"); exit(1); }
//recv
if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1)
{ perror("recv出错!"); exit(1); }
buf[recvbytes] = '';
printf("Received: %s",buf);
::close(sockfd);
}
谢谢!
|
#include