gpt4 book ai didi

c++11 - 错误: invalid use of non-static member function

转载 作者:行者123 更新时间:2023-12-02 11:13:01 25 4
gpt4 key购买 nike

问题:

我试图在另一个类(例如B类)中实例化一个类(例如A类),然后为该实例化对象调用B类中所有A类的成员函数。对于类B中的类A方法的每次调用,我都会收到以下错误消息:

error: invalid use of non-static member function



我知道以下关于同一问题的话题。 Invalid use of non-static member function c++。但是我没有找到与我的问题有关的解决方案。我在下面阐述问题。您能告诉我出什么问题了吗?

码:

以下是我的类(class):

Navigation.h
class Navigation {
public:
Navigation() {
laserData = n.subscribe("/scan", 200, &Navigation::laserCallback, this);
velPub = n.advertise<geometry_msgs::Twist> ("/mobile_base/commands/velocity", 100, this);
}
~Navigation() {}
geometry_msgs::Twist moveCommand();
geometry_msgs::Twist turnCommand();
geometry_msgs::Twist stopCommand();
void laserCallback(const sensor_msgs::LaserScan::ConstPtr& data);
float getObstacleRange();
void broadcastVelocity(geometry_msgs::Twist velocity);

private:
ros::NodeHandle n;
ros::Publisher velPub;
ros::Subscriber laserData;
float obstacleRange;
ros::Timer normalTurnTimer;
ros::Timer driveTimer;
ros::Timer periodicTurnTimer;
geometry_msgs::Twist drivePower;
};

Navigation.cpp
geometry_msgs::Twist Navigation::moveCommand() {
return drivePower;
}

geometry_msgs::Twist Navigation::turnCommand() {
return drivePower;
}

geometry_msgs::Twist Navigation::stopCommand() {
return drivePower;
}

void Navigation::laserCallback(const sensor_msgs::LaserScan::ConstPtr& data) {
obstacleRange = 0.8;

}

float Navigation::getObstacleRange() {
return obstacleRange;
}

void Navigation::broadcastVelocity(geometry_msgs::Twist velocity) {
velPub.publish(velocity);
}

Turtlebot.h
class Turtlebot {
public:
Turtlebot() {
Navigation nomad;
}
~Turtlebot() {}
void drive();

private:
Navigation nomad;
};

Turtlebot.cpp
void Turtlebot::drive() {
if (nomad.getObstacleRange() < 0.7) {
nomad.broadcastVelocity(nomad.stopCommand);
int i;
while (i < 5) {
nomad.broadcastVelocity(nomad.turnCommand);
i++;
}
} else nomad.broadcastVelocity(nomad.moveCommand);
}

错误:

cmake期间出现以下错误。&&使
/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp: In member function ‘void Turtlebot::drive()’:
/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:37:50: error: invalid use of non-static member function
nomad.broadcastVelocity(nomad.stopCommand);
^
/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:40:54: error: invalid use of non-static member function
nomad.broadcastVelocity(nomad.turnCommand);
^
/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:43:53: error: invalid use of non-static member function
} else nomad.broadcastVelocity(nomad.moveCommand);
^
TinyNomad/CMakeFiles/tinynomad.dir/build.make:110: recipe for target 'TinyNomad/CMakeFiles/tinynomad.dir/src/Turtlebot.cpp.o' failed
make[2]: *** [TinyNomad/CMakeFiles/tinynomad.dir/src/Turtlebot.cpp.o] Error 1
CMakeFiles/Makefile2:1191: recipe for target 'TinyNomad/CMakeFiles/tinynomad.dir/all' failed
make[1]: *** [TinyNomad/CMakeFiles/tinynomad.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j12 -l12" failed

最佳答案

首先修复您的构造函数:

Turtlebot() {
nomad = Navigation() ;
}

关于c++11 - 错误: invalid use of non-static member function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53738189/

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