更新this指针对象失败
时间: 2023-07-25 15:22:29 浏览: 44
我不太清楚你的问题的具体情况,需要更多的上下文信息才能够回答你。但是通常情况下,当你使用指针访问对象时,需要确保指针指向的对象是有效的,否则可能会导致程序出现未定义的行为。如果你在更新指针指向的对象时出现了问题,可能是指针为空或指向的内存已经被释放了。此外,如果你正在使用多个指向同一对象的指针,也需要注意避免出现竞争条件。如果你能提供更多的信息,我可以尝试给出更具体的建议。
相关问题
c++中如何把父类的指针指向其子类的指针
在C++中,可以通过向下转型来实现将父类指针转换为子类指针。具体来说,可以使用dynamic_cast操作符将父类指针转换为子类指针。下面是一个示例代码:
```
#include <iostream>
class Parent {
public:
virtual void print() {
std::cout << "This is parent class" << std::endl;
}
};
class Child : public Parent {
public:
void print() override {
std::cout << "This is child class" << std::endl;
}
};
int main() {
Parent* parent = new Child;
Child* child = dynamic_cast<Child*>(parent);
child->print();
delete parent;
return 0;
}
```
在上面的代码中,我们使用了dynamic_cast操作符将Parent类型的指针parent转换为Child类型的指针child,然后调用了child的print()函数,输出了"This is child class"。需要注意的是,使用dynamic_cast进行向下转型时,需要确保父类指针指向的实际对象是子类对象,否则转型会失败,返回null指针。
#include "myapp.h" myapp::myapp(QObject *parent) : QObject(parent) { //创建并打开SQLite数据库 this->mydb = new SqOperator; mydb->openDb(); //创建数据表 mydb->createTable(); //这里分别新建的是登录和注册两个对象 this->login = new mylogin; this->login->show(); this->enroll = new myenroll; this->face = new chatInterface; this->tip = new tips; connect(login,&mylogin::sig_login,this,&myapp::judge); connect(login,&mylogin::sig_enroll,this,&myapp::show_enroll_face); connect(enroll,&myenroll::sig_enroll_info,this,&myapp::insertdb); } bool myapp::judge(QString usrname, QString usrpass) { qDebug()<<usrname<<usrpass; //查询全部数据,并且放入list中 QList<QString> list; mydb->queryTable(list); int i = 0; //当存在两个账号的时候,这里循环里面的if必定会进去,错误和正确都会提示,应该在正确之后直接结束判断,而错误提示则应该放在循环结束 for(i = 0 ; i < list.size() ; i=i+2) { if(usrname == list[i] || usrpass == list[i+1]) { this->face->show(); return true; } } this->tip->loginfail(); return false; } void myapp::show_enroll_face() { this->enroll->show(); } void myapp::insertdb(QString usrname,QString usrpass) { qDebug()<<usrname<<usrpass; info info1; info1.usrname = usrname; info1.usrpass = usrpass; if(mydb->singleInsertData(info1)) { this->tip->insertok(); } }解释每一行代码
这段代码是一个在 Qt 框架下开发的简单的登录和注册应用。以下是每行代码的解释:
1. #include "myapp.h":包含应用程序的头文件。
2. myapp::myapp(QObject *parent) : QObject(parent):myapp 类的构造函数,其中 parent 是 QObject 类的指针。
3. this->mydb = new SqOperator;:创建 SqOperator 类的实例 mydb。
4. mydb->openDb();:打开 SQLite 数据库。
5. mydb->createTable();:创建数据表。
6. this->login = new mylogin;:创建 mylogin 类的实例 login。
7. this->login->show();:显示登录界面。
8. this->enroll = new myenroll;:创建 myenroll 类的实例 enroll。
9. this->face = new chatInterface;:创建 chatInterface 类的实例 face。
10. this->tip = new tips;:创建 tips 类的实例 tip。
11. connect(login,&mylogin::sig_login,this,&myapp::judge);connect(login,&mylogin::sig_enroll,this,&myapp::show_enroll_face);connect(enroll,&myenroll::sig_enroll_info,this,&myapp::insertdb):建立信号和槽的连接。
12. bool myapp::judge(QString usrname, QString usrpass):判断用户名和密码是否正确。
13. qDebug()<<usrname<<usrpass;:输出用户名和密码。
14. QList<QString> list;:创建一个 QList 对象。
15. mydb->queryTable(list);:查询全部数据,并将结果放入 list 中。
16. int i = 0;:初始化循环计数器 i。
17. for(i = 0 ; i < list.size() ; i=i+2):循环遍历 list。
18. if(usrname == list[i] || usrpass == list[i+1]):判断用户名和密码是否正确。
19. this->face->show();:显示聊天界面。
20. return true;:返回真。
21. this->tip->loginfail();:显示登录失败提示。
22. return false;:返回假。
23. void myapp::show_enroll_face():显示注册界面。
24. this->enroll->show();:显示注册界面。
25. void myapp::insertdb(QString usrname,QString usrpass):将用户名和密码插入数据库。
26. qDebug()<<usrname<<usrpass;:输出用户名和密码。
27. info info1;:创建 info 类的实例 info1。
28. info1.usrname = usrname;:将用户名赋值给 info1 的 usrname 成员变量。
29. info1.usrpass = usrpass;:将密码赋值给 info1 的 usrpass 成员变量。
30. if(mydb->singleInsertData(info1)):插入数据到数据库。
31. this->tip->insertok();:显示插入成功提示。