##Qt Creator学习之随记 常见问题(一)
-
遇到的问题:
1.1 编译过后出现如下情况:
error: invalid use of ‘this’ in non-member function this->show(); -
问题分析:
(1)"this"指针的大概作用:在 C++ 中,是指向自己的地址的指针。this指针是所有成员函数的隐含参数。
备注:
"this"指针的参考资料:http://www.runoob.com/cplusplus/cpp-this-pointer.html
(2)根据错误提示:"this->show()"在非成员函数无效使用 -
错误可能出现的情况
(1)使用"this->show()"的函数不是成员函数 而是类对象外部的函数
(2)使用"this->show()"的函数是成员函数 但在定义的时候没有标明是内部函数 -
实例
(4.1)基于3.1 将使用"this->show()"的函数变为类对象的成员函数
(4.2)基于3.2 示例如下:
头文件:
#include <QMainWindow>
#include "window2.h"
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void reshow();
private:
Ui::MainWindow *ui;
private:
window2 *f1;
signals:
void Senddate();
};
#endif // MAINWINDOW_H
在.c中 错误使用"this->show()"如下情况:
void reshow()
{
this->show();
}
这个时候编译过后 系统只会告诉你 "this->show()"在非成员函数无效使用 但是你会发现明明是定义过的成员函数 为什么还是会提示在非成员函数下使用 其实我们只要改成下面的就可以编译执行了
void MainWindow::reshow()
{
this->show();
}