(stu+i)->AddPaymentRecord(record);void Student::AddPaymentRecord(PaymentRecords& record) { payment_records_.push_back(payment); }PaymentRecords::PaymentRecords(const PaymentRecords& p) { this->num = p.num; strcpy_s(this->name, p.name); this->Paytime = p.Paytime; this->cost = p.cost; }引发了异常“:读取访问权限冲突:
时间: 2024-04-22 14:22:25 浏览: 6
这个异常通常是由于访问了私有成员变量或方法而导致的。看起来你在调用`AddPaymentRecord`方法时,将一个`PaymentRecords`对象作为参数传递给了它,但是在`AddPaymentRecord`方法中,你试图访问`payment_records_`私有成员变量,并将`payment`添加到其中。这可能是导致访问权限冲突的原因。
你应该将`payment`作为参数传递给`AddPaymentRecord`方法,并在方法内部将其添加到`payment_records_`容器中,而不是直接访问私有成员变量。
此外,你提供的`PaymentRecords`类的构造函数也可能存在错误。你在构造函数中直接访问了`num`、`name`、`Paytime`和`cost`成员变量。如果这些成员变量是私有的,你需要提供公共的getter和setter方法来访问它们。否则,你将无法在构造函数中访问它们。
相关问题
for (int i = 0; i < sn; i++) { infile2.read(reinterpret_cast<char*>(stu + i), sizeof(*(stu + i))); int size=0; infile2.read(reinterpret_cast<char*>(&size), sizeof(int)); for (int j = 0; j < size; j++) { PaymentRecords record; record.read(infile2); (stu+i)->AddPaymentRecord(record); } }class PaymentRecords { public: PaymentRecords() {}; PaymentRecords(int num, char* n,Date d); PaymentRecords(const PaymentRecords& payment); ~PaymentRecords() {}; void setPayment(int num, char* n, Date d); void setCost(double c); int getNum(); string getName(); string getPaytime(); Date getPayTime(); double getCost(); void read(ifstream& infile); private: int num; char name[20]; Date Paytime; double cost; };PaymentRecords::PaymentRecords(const PaymentRecords& payment) { num = payment.num; strcpy_s(name, payment.name); Paytime = payment.Paytime; cost = payment.cost; }这个复制构造函数调用的时候,进行完赋值操作后就会提示访问权限冲突
这个错误提示是因为在复制构造函数中,你没有显式地声明为 public。默认情况下,类的成员和方法都是私有的,所以复制构造函数也需要声明为 public。你需要在类的定义中将复制构造函数的访问权限设置为 public,例如:
```
class PaymentRecords {
public:
PaymentRecords() {};
PaymentRecords(int num, char* n, Date d);
PaymentRecords(const PaymentRecords& payment);
~PaymentRecords() {};
void setPayment(int num, char* n, Date d);
void setCost(double c);
int getNum();
string getName();
string getPaytime();
Date getPayTime();
double getCost();
void read(ifstream& infile);
private:
int num;
char name[20];
Date Paytime;
double cost;
};
PaymentRecords::PaymentRecords(const PaymentRecords& payment) {
num = payment.num;
strcpy_s(name, payment.name);
Paytime = payment.Paytime;
cost = payment.cost;
}
```
这样就可以解决访问权限冲突的问题了。
#include "mainwindow.h" #include "ui_mainwindow.h" #include<QDebug> #include<QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //登录 stu = new studentt; connect(ui->pushButton,&QPushButton::clicked,this,&MainWindow::logon); connect(stu,&studentt::RturnLogon,this,[=]{ stu->hide(); this->show(); }); //注册 User_Registers = new registers; connect(ui->pushButton_2,&QPushButton::clicked,this,&MainWindow::registerss); connect(User_Registers,®isters::Show_Main,this,[=](){ this->show(); User_Registers->hide(); }); ui->lineEdit_2->setEchoMode(QLineEdit::Password); } MainWindow::~MainWindow() { delete ui; } //数据库链接 void MainWindow::opensql() { db = QSqlDatabase::addDatabase("QODBC"); db.setPort(3306); db.setDatabaseName("sql"); db.setUserName("root"); db.setPassword("123456"); db.open(); } void MainWindow::logon() { //"select * from username where username like '1122' and password like '221'" QString s = "select * from username where username like \'"+ui->lineEdit->text()+"\'"+"and password like \'"+ ui->lineEdit_2->text()+"\'"; opensql(); qDebug()<<s; //账号密码传到数据库执行 result = db.exec(s); if (result.next()) { //判断权限 n = result.value("auth").toInt(); this->hide(); stu->show(); connect(this,&MainWindow::User_Name,stu,&studentt::set_UserName); emit User_Name(ui->lineEdit->text()); } else { QMessageBox::about(this,"提示","账号或密码输入错误请重新输入"); ui->lineEdit->clear(); ui->lineEdit_2->clear(); } } void MainWindow::registerss() { User_Registers->show(); this->hide(); } 分析
这是一个 Qt 框架下的 C++ 程序,主要实现了用户登录和注册的功能。
程序中包含了一个主窗口 MainWindow 和两个子窗口 studentt 和 registers。其中,MainWindow 作为程序的主要窗口,用于用户登录和注册操作,studentt 和 registers 作为子窗口,分别用于实现已登录用户的操作和新用户的注册。
程序中使用了 Qt 的信号与槽机制来实现窗口之间的通信,例如登录操作时,点击登录按钮会触发 MainWindow 中的 logon 函数,该函数会将用户输入的账号和密码传递到数据库中进行验证,如果验证通过则将权限信息保存在变量 n 中,同时隐藏 MainWindow 窗口并显示 studentt 窗口,将用户名传递给 studentt 窗口。
程序中还包含了一些 Qt 的界面控件,例如 QLineEdit 和 QPushButton,用于实现用户输入和按钮点击等操作。
此外,程序中还包含了一个 opensql 函数,用于连接数据库。