error: cannot call member function ‘void me::sendMessage()‘ without object

error: cannot call member function 'void me::sendMessage()' without object

  • 原因分析
  • 解决方案

error: cannot call member function ‘void me::sendMessage()‘ without object_第1张图片

原因分析

在connect中,传递函数地址不用带括号。(参考函数指针的赋值)

#include          // 包含头文件。
using namespace std;        // 指定缺省的命名空间。

void func(int no, string str)
{
	cout << "亲爱的" << no << "号:" << str << endl;
}

int main()
{
	int bh = 3;                                                 // 超女的编号。
	string message = "我是一只傻傻鸟。";    // 向超女表白的内容。

	func(bh, message);

	void (*pfunc)(int, string);           // 声明表白函数的函数指针。
	pfunc = func;                              // 对函数指针赋值,语法是函数指针名=函数名。
	pfunc(bh, message);                  // 用函数指针名调用函数。 C++
	(*pfunc)(bh, message);              // 用函数指针名调用函数。 C语言
}

解决方案

    //按钮绑定mainwindow的槽函数
    connect(ui->myPushButton,&QPushButton::clicked,this,&MainWindow::sendMessage);
    connect(tom,&me::sendMessage,myfriend,&myFriend::receiveMessage);
    connect(tom,&me::sendMessage,myparent,&meParent::receiveMessage);
    connect(tom,&me::sendMessage,myteacher,&myTeacher::receiveMessage);

error: cannot call member function ‘void me::sendMessage()‘ without object_第2张图片

你可能感兴趣的:(Qt开发,qt)