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

文章讲述了在C++编程中遇到的不能调用无对象成员函数的问题,分析了原因在于传递函数地址时不应带括号。通过示例解释了如何正确使用函数指针以及在Qt框架下使用`connect`进行槽函数绑定。解决方案是正确地使用成员函数指针和Qt的信号与槽机制。
摘要由CSDN通过智能技术生成

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

在这里插入图片描述

原因分析

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

#include <iostream>         // 包含头文件。
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);

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 根据你提供的信息,接口出现了AttributeError: 'function' object has no attribute 'items'的错误。这个错误通常表示在代码中使用了一个函数对象,但是该函数对象没有items属性。根据中的描述,你的小伙伴可能是在使用lxml模块写入文件时遇到了这个错误。根据中提到的换成mHandler.sendMessage(),以及中提到的Handler.obtainMessage()和Handler.sendMessage()的区别,我推测可能是在处理消息时使用了错误的方法或者参数。为了解决这个问题,你可以检查代码中涉及到的函数的使用方式,确保正确传递参数并调用正确的方法来处理消息。123 #### 引用[.reference_title] - *1* [已解决AttributeError: ‘functionobject has no attribute ‘ELement](https://blog.csdn.net/yuan2019035055/article/details/126437185)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *2* *3* [报错:AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes](https://blog.csdn.net/qq_45229168/article/details/129150948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值