gpt4 book ai didi

c++ - 初始化对象并将其分配给无指针变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:18 24 4
gpt4 key购买 nike

我在初始化几个对象时遇到问题。我正在编写一个程序,该程序将使用 Player/Stage 模拟 2.0 为移动机器人执行基于边界的探索。我有一个名为 Explorer 的类。我在初始化时遇到困难的对象是 robot、pp、lp。我在网上查看引用页面,我相信这是因为没有赋值运算符,但我希望有另一种方法可以做到这一点。;

这是我的标题

#ifndef EXPLORER_H_
#define EXPLORER_H_
#include <libplayerc++/playerc++.h>
#include <iostream>
#include <fstream>
#include <math.h>`
#include <list>
#include "Map.h"

using namespace PlayerCc;
using namespace std;

struct Pose {
double x;
double y;
double theta;
};

struct Frontier {
int startRow;
int startCol;
int endRow;
int endCol;
double score;
};

class Explorer {

public:
Explorer();
void Explore(Map *map);
void performLaserSweep(Map *map);
void detectandgroupFrontiers(Map *map);
Frontier score_pick_Frontier();
void goToFrontier(Frontier f);

private:
PlayerClient robot;
Position2dProxy pp;
LaserProxy *lp;
Pose pose;
list<Frontier> unexploredFrontiers;
};

#endif /* EXPLORER_H_ */

这是我的 .cc 文件,所有重要的是构造函数,所以这就是我要展示的所有内容

#include "Explorer.h"

Explorer::Explorer() {

robot = new PlayerClient("127.0.0.1", 6665);
pp = new Position2dProxy(robot, 0);
lp = new LaserProxy(robot, 0);
if (lp == NULL) {
cerr << "Error initializing LASER" << endl;
exit(1);
}
pp.SetMotorEnable(true);
}

提前感谢您的帮助

这是编译错误

Explorer.cc: In constructor ‘Explorer::Explorer()’:
Explorer.cc:11: error: no matching function for call to ‘PlayerCc::Position2dProxy::Position2dProxy()’
/usr/include/player-2.0/libplayerc++/playerc++.h:1566: note: candidates are: PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient*, uint)
/usr/include/player-2.0/libplayerc++/playerc++.h:1553: note: PlayerCc::Position2dProxy::Position2dProxy(const PlayerCc::Position2dProxy&)
Explorer.cc:13: error: base operand of ‘->’ has non-pointer type ‘PlayerCc::PlayerClient’
Explorer.cc:13: error: expected unqualified-id before ‘new’
Explorer.cc:13: error: expected ‘;’ before ‘new’
Explorer.cc:14: error: no matching function for call to ‘PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient&, int)’
/usr/include/player-2.0/libplayerc++/playerc++.h:1566: note: candidates are: PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient*, uint)
/usr/include/player-2.0/libplayerc++/playerc++.h:1553: note: PlayerCc::Position2dProxy::Position2dProxy(const PlayerCc::Position2dProxy&)
Explorer.cc:15: error: no matching function for call to ‘PlayerCc::LaserProxy::LaserProxy(PlayerCc::PlayerClient&, int)’
/usr/include/player-2.0/libplayerc++/playerc++.h:900: note: candidates are: PlayerCc::LaserProxy::LaserProxy(PlayerCc::PlayerClient*, uint)
/usr/include/player-2.0/libplayerc++/playerc++.h:881: note: PlayerCc::LaserProxy::LaserProxy(const PlayerCc::LaserProxy&)
make: *** [all] Error 1

最佳答案

Explorer 类中的

robot 不是指针,但您正尝试使用 new 关键字对其进行初始化:

 robot = new PlayerClient("127.0.0.1", 6665); // this won't work

与变量 pp 相同。

关于您收到的错误的注释之一:注意:候选人是:PlayerCc::Position2dProxy::Position2dProxy(PlayerCc::PlayerClient*, uint) 还建议构造函数需要一个PlayerClient 指针。

在 Explorer 类中试试这个:

PlayerClient *robot;

不要忘记在完成后删除它。

发现此类错误的一种简单方法是仔细查看错误消息。当错误显示 error: base operand of '->' has non-pointer type 时,它只是意味着您正试图在某些东西上使用指针运算符 ->不是指针。

关于c++ - 初始化对象并将其分配给无指针变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16409046/

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