【问题标题】:error: invalid use of non-static member function错误:无效使用非静态成员函数
【发布时间】:2019-05-13 06:32:52
【问题描述】:

问题:

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

错误:非静态成员函数的使用无效

我知道以下关于同一问题的线程。 Invalid use of non-static member function c++。但我没有找到与我的问题相关的解决方案。我正在详细说明下面的问题。你能告诉我出了什么问题吗?

代码:

以下是我的课程:

导航.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;
};

导航.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 .. && make

期间收到以下错误
/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

【问题讨论】:

  • 您能否删除尽可能多的代码以尽量减少问题?
  • 好的。我删除了一些不必要的代码。
  • 方法broadcastVelocity接受(非函数)类型geometry_msgs::Twist的参数。您正在尝试使用 nomad.stopCommand 调用此方法,这是一个返回所需类型值的 函数。编译器确切地抱怨这种不兼容性。您可能想调用该函数:nomad.stopCommand().
  • 谢谢。此解决方案有效。这正是问题所在。

标签: c++11 cmake compiler-errors member-functions non-static


【解决方案1】:

从修复你的构造函数开始:

Turtlebot() {
        nomad = Navigation() ;
    }

【讨论】:

  • 我试过这个解决方案。但我仍然得到同样的错误。
  • t那么这个n答案如何??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 2013-06-27
  • 2017-05-19
相关资源
最近更新 更多