这可能是一个重复的问题,但已建立的答案并没有解决我的问题。我正在尝试在 QT 中创建一个客户端服务器应用程序,其中客户端向服务器发送一个字符串消息代码,服务器必须连接到 sql server 数据库并检索与客户端消息代码相关的数据。下面是我到目前为止编写的服务器部分。但我收到此错误:无法打开包含文件:'QtSql':没有这样的文件或目录。当我制作单独的项目并运行数据库部分时,它工作得很好,但是当我把它们放在一起时,它就失败了。任何人都有一些建议可以解决这个问题?
ServerSocket.pro
QT += core
QT += network
QT += sql
QT -= gui
TARGET = ServerSocket
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
server.cpp
HEADERS += \
server.h
Run Code Online (Sandbox Code Playgroud)
服务器套接字.h
#ifndef SERVER_H
#define SERVER_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
class Server : public QObject
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
signals:
public slots:
void newConnection();
private:
QTcpServer *server;
};
#endif // SERVER_H
Run Code Online (Sandbox Code Playgroud)
服务器套接字.cpp
#include …Run Code Online (Sandbox Code Playgroud) 考虑下面的代码,如果我使用这样的Die类实例会发生什么:
Die d;
d.Roll(20);
d.Roll(15);
d.Roll(30);
Run Code Online (Sandbox Code Playgroud)
在为内存再次分配内存之前,我应该还是不应该释放值占用的内存?delete[ ]之前new?
die.h
#ifndef DIE_H
#define DIE_H
#include<iostream>
#include<time.h>
using namespace std;
class Die
{
private:
int number;
int* values;
int count;
void roll();
public:
Die(){srand(static_cast<int>(time(NULL)));number=0;values=NULL;count=0;}
void Roll(int n);
int getNumber()const{return number;}
void printLastValue();
void printValues();
~Die(){delete [] values;}
};
#endif
Run Code Online (Sandbox Code Playgroud)
die.cpp
#include"die.h"
#include<iostream>
#include<time.h>
using namespace std;
void Die::roll()
{
number=1+rand()%6;
}
void Die::printLastValue()
{
cout<<number<<endl;
}
void Die::Roll(int n)
{
count=n;
values=new int[count];
for(int i=0;i<count;i++)
{ …Run Code Online (Sandbox Code Playgroud) 我知道这个话题已经讨论过,但我花了半天时间来弄清楚我做错了什么.我一直有这个错误,我想我已经完成了数以千计的语法检查.谁能帮我?
#ifndef ADDEDITDIALOG_H
#define ADDEDITDIALOG_H
#include <QDialog>
#include <QStandardItemModel>
#include "characteristics.h"
#include "equipment.h"
#include "generaldata.h"
#include "registration.h"
#include "revisiondialog.h"
#include "vehicle.h"
#include "vehiclehelper.h"
#include "verification.h"
#include "verificationdialog.h"
namespace Ui {
class AddEditDialog;
}
class AddEditDialog : public QDialog
{
Q_OBJECT
public:
explicit AddEditDialog(QWidget *parent = 0);
~AddEditDialog();
bool getIsNew() const;
bool getIsEdited() const;
bool getAddRevision() const;
Vehicle getVehicleToAdd() const;
void setVehicleToAdd();
void loadVehicleToEdit(Vehicle car);
void createTableView(const QList<Verification> list);
QList<Verification> getRevisionsList() const;
private slots:
void on_add_revision_clicked();
void on_save_clicked();
void on_cancel_clicked();
void …Run Code Online (Sandbox Code Playgroud) 我必须创建一个类直方图并在这个类上进行操作.输入可以是一维阵列或二维阵列.当我将数组转换为矩阵时出现问题.这是我到目前为止所尝试的.错误是<Unable to read memory>
histrogram.h
#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#include<iostream>
class Histogram
{
private:
int** matrix;
int lines;
void SortMatrix();
public:
Histogram(){ }
Histogram(int elements[], int elementsNr);
Histogram(int** m, int l);
void Print();
};
#endif
Run Code Online (Sandbox Code Playgroud)
historgram.cpp
#include"histogram.h"
using namespace std;
Histogram::Histogram(int** m, int l)
{
matrix=m;
lines=l;
SortMatrix();
}
Histogram::Histogram(int elements[], int elementsNr)
{
lines=0;
//initialize matrix : elementrNr lines and 2 columns
int** matrix=new int*[elementsNr];
for(int i=0;i<elementsNr;i++)
{
matrix[i]=new int[2];
matrix[i][0]=INT_MIN;
matrix[i][1]=INT_MIN;
}
//search each element from the …Run Code Online (Sandbox Code Playgroud) 为什么rand()生成相同的数字?
die.h
#ifndef DIE_H
#define DIE_H
class Die
{
private:
int number;
public:
Die(){number=0;}
void roll();
int getNumber()const{return number;}
void printValue();
};
#endif
Run Code Online (Sandbox Code Playgroud)
die.cpp
#include"die.h"
#include<iostream>
#include<time.h>
using namespace std;
void Die::roll()
{
srand(static_cast<int>(time(0)));
number=1+rand()%6;
}
void Die::printValue()
{
cout<<number<<endl;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include"die.h"
#include<iostream>
using namespace std;
int main()
{
Die d;
d.roll();
d.printValue();
d.roll();
d.printValue();
d.roll();
d.printValue();
}
Run Code Online (Sandbox Code Playgroud) 我正在遍历列表,我想使用const_iterator.但我不知道该怎么做!到现在为止我使用了iterator并以这种方式实现了我的功能.任何人都可以帮我使用const_iterator吗?
void MainWindow::populate(const QList<Vehicle> &vehicles)
{
int j = 0;
QListIterator<Vehicle> iter(vehicles);
while(iter.hasNext()){
Vehicle car = iter.next();
//set car
QString makeAndModel = car.getGeneralData().getMake() + car.getGeneralData().getModel();
QStandardItem *mAndM = new QStandardItem(QString(makeAndModel));
mAndM->setEditable(false);
model->setItem(j,0,mAndM);
//set type
QStandardItem *type = new QStandardItem(QString(car.getGeneralData().getType()));
type->setEditable(false);
model->setItem(j,1,type);
//set mileage
QString mileageString = QString::number(car.getGeneralData().getMileage());
QStandardItem *mileage = new QStandardItem(QString(mileageString));
mileage->setEditable(false);
model->setItem(j,2,mileage);
//set year
QString yearString = QString::number(car.getGeneralData().getYear());
QStandardItem *year = new QStandardItem(QString(yearString));
year->setEditable(false);
model->setItem(j,3,year);
//set registration
QString regString = VehicleHelper::convertBoolToString(car.getRegistration().isRegistered());
QStandardItem *regDate = new QStandardItem(QString(regString));
regDate->setEditable(false);
model->setItem(j,4,regDate); …Run Code Online (Sandbox Code Playgroud) 调试后我收到此错误消息:正常块后检测到HEAP CORRUPTION(#55).如果我增加阵列的大小(no_of_days+1),它正在工作,但我不知道它是否正确.我做错了什么?
int main()
{
int no_of_days=0;
int no_operations=0;
int *a;
int i;
FILE *pfile1=NULL;
FILE *pfile2=NULL;
char *filename1="input.txt";
char *filename2="output.txt";
pfile1=fopen(filename1, "r");
if(pfile1==NULL)
{
printf("Error on opening the file %s.",filename1);
return 0;
}
fscanf(pfile1,"%d%d",&no_of_days,&no_operations);
a=(int*)malloc((no_of_days)*sizeof(int));
if(a==NULL)
{
printf("Memory allocation failed.");
return 0;
}
for(i=1;i<=no_of_days;i++)
{
fscanf(pfile1,"%d",&a[i]);
}
pfile2=fopen(filename2,"a");
if(pfile2==NULL)
{
printf("Memory allocation failed!",filename2);
}
for(i=1;i<=no_of_days;i++)
{
fprintf(pfile2,"%d",a[i]);
}
free(a);
fclose(pfile1);
fclose(pfile2);
Run Code Online (Sandbox Code Playgroud) 我@OneToOne在以下表格之间存在双向关系:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table
public class StudentAccount implements DomainModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer idstudentaccount;
@OneToOne(optional = false)
@JoinColumn(name = "idstudent")
private Student student;
private String username;
private String password;
//getters and setters
}
@Entity
@Table(name = "student")
public class Student implements DomainModel {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer idStudent;
private String firstname;
private String lastname;
@OneToOne(mappedBy = "student", cascade = CascadeType.ALL, targetEntity=StudentAccount.class)
private …Run Code Online (Sandbox Code Playgroud)