gpt4 book ai didi

c++ - 如何解决访问不同函数的 "nonstatic member reference must be relative to a specific object"

转载 作者:行者123 更新时间:2023-11-28 04:17:35 25 4
gpt4 key购买 nike

我正在尝试将一些用于测试的函数传递到 main() 节点中,如下所示。但是,我得到以下信息 non static member reference must be relative to a specific object。我正在对这个问题进行大量研究,例如我发现 this也有类似的问题this is useful那是在解释如何访问和理解与“父”实例的关系。我应用了建议 here多亏了传递 madgwick::MADGWICK_reader obj(std::string file); 在主体上,我能够部分访问具有函数 class MADGWICK_reader >void filter_function(const sensor_msgs::Imu &msg); 我正在尝试从 main 访问,但仍然有些地方不完全正确。

我知道通过简单地添加“static”关键字来使成员静态化是不正确的,我也需要定义它们,这就是为什么函数专门设置为void的原因filter_function(const sensor_msgs::Imu &msg);我传递 ampersend & 是为了通过对 main 的引用传递它,但似乎从未收到该函数。

这是 ma​​dgwick_filter.h,注意 void filter_function(const sensor_msgs::Imu &msg); 函数在 main< 中有问题:

namespace madgwick
{
using timestamp_t = uint64_t;
using timestampToDouble_t = double;

struct IMU_MADGWICK_DATA
{
double magz;
double magy;
double magx;
float accelz;
float accelx;
float accely;
unsigned long timestamp;
double phi; // orientation
double psi; // orientation
double theta; // orientation
double gyrox; // angular velocity x
double gyroy; // angular velocity y
double gyroz; // angular velocity z
};


class Madgwick
{
public:
Madgwick();

};

class MADGWICK_reader
{
public:
MADGWICK_reader(std::string filename);
bool nextLine();
bool nextLineWithSupport();

IMU_MADGWICK_DATA madgwick_data;

sensor_msgs::Imu imuMadgwickMsg;
sensor_msgs::MagneticField magMedgwickMsg;

geometry_msgs::QuaternionStamped q;
geometry_msgs::Vector3Stamped v;
geometry_msgs::TransformStamped q_trans;
float sampleFreq;
double beta;
double q0=1.0, q1=0.0, q2=0.0, q3=0.0;
std_msgs::Header header;
float ax, ay, az, gx, gy, gz;
ros::Duration dtime;
float dt;

float invSqrt();
void qua2Euler(geometry_msgs::QuaternionStamped);
void madgwickIMU(float gx, float gy, float gz, float ax, float ay, float az);
void filter_function(const sensor_msgs::Imu &msg);

private:
io::CSVReader<13> madg_imu_reader;
unsigned int imuMadgNum;
unsigned int magMadgNum;
void packMadgMagMsg();
void pack_Imu_Madgwick_Msg();
void pack_Mag_Madgwick_Msg();
};
}

ma​​dgwick_filter.cpp

namespace madgwick
{
Madgwick::Madgwick()
{

}

MADGWICK_reader::MADGWICK_reader(std::string filename):
madg_imu_reader(filename)
{
// operations ...
}

void MADGWICK_reader::filter_function(const sensor_msgs::Imu &msg)
{
timestampToDouble_t currentTime = (madgwick_data.timestamp)/1e6;
ros::Time stamp(currentTime);
header = msg.header;
ax = float(msg.linear_acceleration.x);
ay = float(msg.linear_acceleration.y);
az = float(msg.linear_acceleration.z);

// more operations ...
}
}

最后是 ma​​dgwick_filter_node.cpp

#include "imu_filter_madgwick/madgwick_filter.h"
#include <iostream>

int main(int argc, char** argv)
{
ros::init(argc, argv, "madgwick_filter_node");
madgwick::MADGWICK_reader reader(std::string filename);

ros::NodeHandle n;
ros::Publisher pub1 = n.advertise<geometry_msgs::QuaternionStamped>("quaternion", 1);
ros::Publisher pub2 = n.advertise<geometry_msgs::Vector3Stamped>("ypr", 1);

tf::TransformBroadcaster q_brodecaster;

madgwick::MADGWICK_reader obj(std::string file);

ros::Subscriber sub = n.subscribe("imu0", 10, obj.filter_function); // <-- Error Here

ros::spin();
}

谁能指出正确的方向或解释为什么仍然无法从 main 访问类中包含的函数?感谢您阐明这一点。

最佳答案

由于您没有提供订阅声明,但您的问题似乎与将类成员函数传递给其他函数有关,因此下面的解决方案提供了两种实现方法,一种通过 lambda,另一种通过绑定(bind)。

#include <iostream>
#include <functional>
class Test{
public:
void fun(int x)
{
std::cout<<"I am having fun in Test with number "<<x<<std::endl;
}
};
void sampleFunction(std::function<void(int)> sf,int x)
{
sf(x);
}
int main()
{
Test obj;
auto objf = std::bind(&Test::fun,&obj,std::placeholders::_1);
sampleFunction(objf,5);

auto lf = [&obj](int y) { return obj.fun(y); };
sampleFunction(lf,6);
return 0;
}

关于c++ - 如何解决访问不同函数的 "nonstatic member reference must be relative to a specific object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56263914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com