char totalbuffer[2048] = {0};
这是我想要的数组的初始化。我试图放入 main.cpp 和我的头文件,qt 创建者不断显示错误。
这是我的 .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);
}
焦点为 char totalbuffer[2048]={0}; ,我真的不知道我应该把它放在哪里。我用谷歌和stackoverflow搜索过,所有的方法都是在.cpp文件中使用类和调用。但我的一个是 GUI 应用程序,这与控制台不同。
我应该怎么办?我真的需要帮助。感谢大家的帮助,谢谢~
已编辑***这是错误显示。
error assigning to an array from an initializer list
我认为初始化列表不能放入头文件和 main.cpp 那么我将如何初始化一个数组?像 C++ Visual Studio 一样使用公共变量......