如何解决 "nonstatic member reference must be relative to a specific object" 访问不同的功能
how a "nonstatic member reference must be relative to a specific object" can be solved accessing different function
我正在尝试将一些测试功能传递到 main()
节点中,如下所示。
但是,我得到以下 non static member reference must be relative to a specific object
。
我正在对这个问题进行大量研究,例如我发现 this that had a similar problem, also this is useful that was explaining how to access and understand the relationship with the "parent" instance. I applied what was advised 并且由于在主节点上传递 madgwick::MADGWICK_reader obj(std::string file);
我能够部分访问 class MADGWICK_reader
具有我试图从 main
访问的函数 void filter_function(const sensor_msgs::Imu &msg);
但仍然有些地方不完全正确。
我知道通过简单地添加"static"关键字使成员静态化是不正确的,我也需要定义它们,这就是为什么函数被专门设置为void filter_function(const sensor_msgs::Imu &msg);
我传递 ampersend &
是为了通过对 main
的引用传递它,但似乎从未收到该函数。
这是 madgwick_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();
};
}
madgwick_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 ...
}
}
最后是 madgwick_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
访问 class 中包含的功能吗?感谢您阐明这一点。
由于您没有提供订阅声明,但您的问题似乎与将 class 成员函数传递给其他函数有关,因此下面的解决方案提供了两种实现方式,一种是通过 lambda 表达式,另一种是通过绑定。
#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;
}
我正在尝试将一些测试功能传递到 main()
节点中,如下所示。
但是,我得到以下 non static member reference must be relative to a specific object
。
我正在对这个问题进行大量研究,例如我发现 this that had a similar problem, also this is useful that was explaining how to access and understand the relationship with the "parent" instance. I applied what was advised madgwick::MADGWICK_reader obj(std::string file);
我能够部分访问 class MADGWICK_reader
具有我试图从 main
访问的函数 void filter_function(const sensor_msgs::Imu &msg);
但仍然有些地方不完全正确。
我知道通过简单地添加"static"关键字使成员静态化是不正确的,我也需要定义它们,这就是为什么函数被专门设置为void filter_function(const sensor_msgs::Imu &msg);
我传递 ampersend &
是为了通过对 main
的引用传递它,但似乎从未收到该函数。
这是 madgwick_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();
};
}
madgwick_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 ...
}
}
最后是 madgwick_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
访问 class 中包含的功能吗?感谢您阐明这一点。
由于您没有提供订阅声明,但您的问题似乎与将 class 成员函数传递给其他函数有关,因此下面的解决方案提供了两种实现方式,一种是通过 lambda 表达式,另一种是通过绑定。
#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;
}