:-1: 错误:collect2: ld returned 1 exit status错误,急

solomon1989 2011-11-21 10:14:59
最近在弄一个qt局域网聊天的简单例子,我参照网上的基于qt编写的c/s模型的简单聊天程序


/*login.cpp*/

#include "login.h"

LoginDialog::LoginDialog()
{
setupUi(this);
connect(awayButton, SIGNAL(clicked()),qApp, SLOT(quit()));
connect(enterButton, SIGNAL(clicked()), this, SLOT(enterSlot()));
}

void LoginDialog::enterSlot()
{

if (lineEdit_2->text().isEmpty())
{
QMessageBox mess;
mess.setFont(QFont("Sans Serif", 12, 50));
mess.warning(this,
QString::fromLocal8Bit("出错"),
QString::fromLocal8Bit("<font size=5>请输入正确的昵称,谢谢!</font>"));

return;
}
emit sendEnterMessage(lineEdit_2->text(), lineEdit_3->text());
emit showChatWindow();
}

void LoginDialog::closeEvent(QCloseEvent *)
{
qApp->quit();
}

#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QDataStream>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
serverSocket = NULL;
server = NULL;
client = NULL;
login = new LoginDialog;
login->show();
connect(login, SIGNAL(showChatWindow()), this, SLOT(showAndHideSlot()));

connect(login, SIGNAL(sendEnterMessage(QString , QString)),
this,SLOT(enterSlot(QString , QString )));

connect(ui->writeMessageEdit, SIGNAL(textChanged()), this, SLOT(changeButtonStateSlot()));

connect(ui->aboutButton, SIGNAL(clicked()), this, SLOT(createAboutSlot()));

connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(appendMessageSlot()));

connect(ui->quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
}

Widget::~Widget()
{
delete ui;
}


void Widget::showAndHideSlot()
{
delete login;
this->show(); //显示聊天窗口
}

void Widget::createAboutSlot()
{}

void Widget::enterSlot(QString name, QString host)
{
port = 22222;

if (host.isEmpty())
{
socketServer();
}
else
{
socketClient(host);
}

userName = name;
ui->onlineMessageList->addItem(name);
}

void Widget::changeButtonStateSlot()
{
bool boo_dis = ui->writeMessageEdit->toPlainText().isEmpty();
ui->sendButton->setDisabled(boo_dis);
}

void Widget::appendMessageSlot()
{
QString content = ui->writeMessageEdit->toPlainText();
if (content.isEmpty())
{
QMessageBox::warning(this, "出错", QString::fromLocal8Bit("发送的内容不能为空"));
return;
}
nowDateTime = QDateTime::currentDateTime();
ui->showMessageEdit->append(
QString("\n[%1] %2 %3\n%4")
.arg(nowDateTime.toString("yyyy-MM-dd hh:mm:ss"))
.arg(userName)
.arg(QString::fromLocal8Bit("说道:"))
.arg(content) );

QDataStream out;
if (serverBool)
{
out.setDevice(serverSocket);
}
else if (!serverBool)
{
out.setDevice(client);
}
int mark = 0;
out << mark;
out << userName << content;
ui->writeMessageEdit->clear();
}

void Widget::socketServer()
{
serverBool = true;
server = new QTcpServer(this);
serverSocket = new QTcpSocket(this);
server->listen(QHostAddress::Any, port);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
}

void Widget::newConnectionSlot()
{
serverSocket = server->nextPendingConnection();
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(newDataSlot()));
connect(serverSocket, SIGNAL(disconnected()), this, SLOT(deleNameSlot()));
}

void Widget::socketClient(QString host)
{
serverBool = false;
client = new QTcpSocket(this);
client->connectToHost(host, port);
connect(client, SIGNAL(connected()), this, SLOT(addSlot()));
connect(client, SIGNAL(readyRead()), this, SLOT(newDataSlot()));
connect(client, SIGNAL(disconnected()), this, SLOT(deleNameSlot()));
}

void Widget::addSlot()
{
QString myName = userName;
QString content;
QDataStream out(client);
int mark = 11;
out << mark;
out << myName << content;
}


void Widget::newDataSlot()
{
int mark;
QString hisName;
QString content;
QDataStream in;
if (serverBool)
{
in.setDevice(serverSocket);
}
else if (!serverBool)
{
in.setDevice(client);
}
in >> mark;


// 数据解析
if (mark == 11) //addList
{
in >> hisName >> content;
if (content.isEmpty())
{
ui->onlineMessageList->addItem(hisName);
if (serverBool)
{
QString content;
QDataStream out(serverSocket);
int mark = 11;
out << mark;
out << userName<< content;
}
}
return;
}

else if (mark == 0) //message
{
in >> hisName >> content;
nowDateTime = QDateTime::currentDateTime();
ui->showMessageEdit->append(
QString("\n[%1] %2 %3\n%4")
.arg(nowDateTime.toString("yyyy-MM-dd hh:mm:ss"))
.arg(hisName)
.arg(QString::fromLocal8Bit("说道:"))
.arg(content) );
return;
}
}

void Widget::deleNameSlot()
{

ui->onlineMessageList->takeItem(1);
}


void Widget::keyPressEvent(QKeyEvent *event) //ctrl+s
{
if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S)
{
appendMessageSlot();
}
else
{
event->ignore();
}
}

void Widget::closeEvent(QCloseEvent *event) //close application event
{
if (server != NULL)
{
server->close();
}
if (client != NULL)
{
client->close();
}
if (serverSocket != NULL)
{
serverSocket->close();
}
event->accept();
}


,核心代码是这些,我在此基础上加了gui框架,用的是ui可视化编辑,但是现在编译:-1: 错误:collect2: ld returned 1 exit status
下面是我加的部分

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QTcpServer>
#include <QString>
#include <QDateTime>
#include <QKeyEvent>
#include "login.h"
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();


private:
Ui::Widget *ui;
LoginDialog *login;
QTcpServer *server;
QTcpSocket *client;
QTcpSocket *serverSocket ;
int port;
QString userName;
QDateTime nowDateTime;
bool serverBool;

private slots:
void showAndHideSlot();
void enterSlot(QString name, QString host);
void createAboutSlot() ;
void changeButtonStateSlot() ;
void appendMessageSlot() ;
void socketServer();
void newConnectionSlot() ;
void socketClient(QString host) ;
void addSlot() ;
void newDataSlot() ;
void deleNameSlot() ;
void keyPressEvent(QKeyEvent *event);
void closeEvent(QCloseEvent *event);

};

#endif // WIDGET_H

#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
#include <QLineEdit>
namespace Ui {
class LoginDialog;
}

class LoginDialog : public QDialog
{
Q_OBJECT

public:
explicit LoginDialog();
~LoginDialog();

private:
Ui::LoginDialog *ui;
private slots:
void enterSlot();
void closeEvent(QCloseEvent *);
signals:
void sendEnterMessage(QString, QString);
void showChatWindow();
};



#endif // LOGIN_H
工程文件是这么写的

######################################################################
# Automatically generated by qmake (2.01a) ??? ??? 21 09:45:35 2011
######################################################################

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += login.h widget.h
FORMS += LoginDialog.ui widget.ui
SOURCES += login.cpp main.cpp widget.cpp
QT+=network

QT += core gui network
...全文
207 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
我是学友 2011-11-21
  • 打赏
  • 举报
回复
这个错误多是定义了函数却没有实现
erik1810 2011-11-21
  • 打赏
  • 举报
回复
看看是否是编译问题

16,238

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧