char totalbuffer[2048] = {0};
這是我想要的數組的初始化。我試圖把main.cpp和我的頭文件,qt創建者保持顯示錯誤。如何在qt GUI應用程序中用null初始化數組?
這是我的.h頭文件代碼:
#ifndef QTPROJECT2_H
#define QTPROJECT2_H
#include <QDialog>
#include <QMainWindow>
#include <QtNetwork/QHostAddress>
#include <QLabel>
#include <QPushButton>
#include <QUdpSocket>
#include <QString>
#include <QTcpSocket>
#include <QDataStream>
#include <qstring.h>
#include <QStandardItem>
namespace Ui {
class QtProject2;
}
class QtProject2 : public QDialog
{
Q_OBJECT
public:
explicit QtProject2(QWidget *parent = 0);
~QtProject2();
void start(QString address, quint16 port);
char totalbuffer[2048]={0};
QStandardItemModel* ListModel;
private slots:
void on_pushButton_clicked();
public slots:
void startTransfer();
void disconnected();
void readyRead();
signals:
void socketReady();
private:
Ui::QtProject2 *ui;
QTcpSocket *client;
};
#endif // QTPROJECT2_H
這是我的main.cpp編碼:
#include "qtproject2.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtProject2 w;
w.show();
char totalbuffer[2048]={0};
return a.exec();
}
這是我的項目qtproject2.cpp: 的#include「qtproject2.h 「 的#include 」ui_qtproject2.h「
QtProject2::QtProject2(QWidget *parent) :
QDialog(parent),
ui(new Ui::QtProject2)
{
ui->setupUi(this);
}
QtProject2::~QtProject2()
{
delete ui;
QtProject2 Client2;
Client2.close();
}
void QtProject2::startTransfer()
{
ui->slabel->setText("Connected!");
}
void QtProject2::disconnected()
{
ui->slabel->setText("Disconnected!");
}
void QtProject2::readyRead()
{
client->waitForBytesWritten(1000);
client->waitForReadyRead(3000);
char CustomerData_MacAdd[][18]={"14:13:12:11:67:11","52:22:22:22:22:22", "14:22:44:55:22" };
char CustomerData_Username[][10]={"Robert","Alex","Ivan"};
QByteArray buffer1 = client->readLine();
char *temp = buffer1.data();
char buffer[1024]={0};
if (strncmp(temp,"*CLIENT",6)==0)
{
int j;
for(j=9;j<26;j++)
{ buffer[j-9]=temp[j];}
}else if(strncmp(temp,"*ALERT",5)==0)
{
int j;
for(j=8;j<25;j++)
{buffer[j-8]=temp[j];}
}
//char totalbuffer[2048]={0};
int k;
for (k=0;k<3;k++){
if (strncmp(buffer,CustomerData_MacAdd[k],16)==0){
strncat(totalbuffer,"User is in Queue : ",19);
strncat (totalbuffer,CustomerData_Username[k],size_t(CustomerData_Username[k]));
strncat(totalbuffer,"\n",2);
ui->label->setText(totalbuffer);
}}
ui->slabel->setText(buffer1);
}
void QtProject2::on_pushButton_clicked()
{
client = new QTcpSocket(this);
connect(client, SIGNAL(connected()),this, SLOT(startTransfer()));
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
/*Connect to User Define Port Address*/
client->connectToHost("127.0.0.1", 1234);
}
焦點爲CH ar totalbuffer [2048] = {0}; ,我真的不知道我該把它放在哪裏。 我已經用谷歌和stackoverflow搜索,所有的方法是使用類和調用.cpp文件。但我的一個是GUI應用程序,這與控制檯不同。
我該怎麼辦?我真的需要幫助。欣賞的幫助和謝謝你們〜
編輯*** 這是錯誤顯示。
error assigning to an array from an initializer list
我覺得初始化列表不能放在頭文件和main.cpp的,所以我要如何初始化數組?像C++使用公共變量的Visual Studio ....
你爲什麼不告訴我們錯誤?另外,您應該使用'QString'或'QByteArray'來操作char數組。 –
顯示*什麼*錯誤?請使用更完整的代碼編輯您的問題(最好是[SSCCE](http://sscce.org/)),並且請添加完整和未編輯的錯誤列表。 –
對不起......我編輯過。我正在開發一個qt GUI應用程序,有一個main.cpp qtproject2.h qtproject2.cpp 我不知道在哪裏放置公共初始化列表 –