简体   繁体   English

当两个成员都在同一类中时,出现错误“非静态成员引用必须相对于特定对象”

[英]Getting error “a nonstatic member reference must be relative to a specific object” while both member are in the same class

I know this question had been asked many times but I'm still stuck. 我知道这个问题已经被问过很多次了,但我仍然坚持。 I thought I got what this error was about what obviously I don't. 我以为我知道这个错误是关于我显然没有的错误。

So, the error I am getting is 所以,我得到的错误是

a nonstatic member reference must be relative to a specific object

My code is: 我的代码是:

class theTranslator {
public:
    ros::NodeHandle nh;

    ros::Publisher pub = nh.advertise<sensor_msgs::Image>("camera/depth/image_raw", 100);

    static void getMessage(const sensor_msgs::Image::ConstPtr& recMmsg) {
        ROS_INFO( "I heard message" );
        pub.publish(recMmsg); //*** ERROR IS HERE ***
    }
};

since pub is part of the same class as getMessage() , shouldn't it work? 由于pubgetMessage()是同一类的一部分,所以它不起作用吗? How can I make a static member function use a variable member of the same class? 如何使static成员函数使用同一类的可变成员?

PS this is done in ROS (Robotics Operating System) but I believe this is a C++ mistake (not related to ROS). PS这是在ROS (机器人操作系统)中完成的,但我认为这是C ++错误(与ROS不相关)。

In C++ you can not access a non static class member from a static method. 在C ++中,您不能从静态方法访问非静态类成员。 Make it a normal method and try like below:- 将其设为正常方法,然后尝试如下操作:-

 void getMessage(const sensor_msgs::Image::ConstPtr& recMmsg){
        ROS_INFO( "I heard message" );
        pub.publish(recMmsg); //*** ERROR IS HERE ***
    }

Else declare pub as static member 否则将pub声明为静态成员

static ros::Publisher pub; 

Also refer to the below answer 另请参阅以下答案

C++ static member functions and variables C ++静态成员函数和变量

You need to make getMessage non-static or you need to make pub static. 您需要使getMessage为非静态,或者需要使pub静态。

Probably what you really need to do is rethink your design. 您可能真正需要做的是重新考虑您的设计。 Why are you trying to make getMessage static? 您为什么要尝试使getMessage静态? As a very general rule of thumb static is a mistake, especially for a newbie. 作为非常普遍的经验法则,静态是一个错误,特别是对于新手而言。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类不断实例化 - 从同一个 class 调用 function - 非静态成员引用必须相对于特定的 object - Classes keep getting instantiated - calling a function from the same class - a nonstatic member reference must be relative to a specific object 非静态成员引用必须相对于特定对象? - A nonstatic member reference must be relative to a specific object? 非静态成员引用必须相对于特定的 object - A nonstatic member reference must be relative to a specific object 非静态成员引用必须相对于特定对象(父类问题) - a nonstatic member reference must be relative to a specific object (parent class problems) 错误:非静态成员引用必须相对于特定对象 - error:a nonstatic member reference must be relative to a specific object 编译器错误:“非静态成员引用必须相对于特定对象” - Compiler error: “a nonstatic member reference must be relative to a specific object” C ++错误非静态成员引用必须相对于特定对象 - C++ error A nonstatic member reference must be relative to a specific object 错误“非静态成员引用必须相对于特定的 object - ERROR "nonstatic member reference must be relative to a specific object 一个新的新类对象的功能; 错误:非静态成员引用必须相对于特定对象 - The function of a new new class object; error: A nonstatic member reference must be relative to a specific object C ++非静态成员引用必须相对于特定对象 - C++ A nonstatic member reference must be relative to a specific object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM