关于重载<<和重载=的bug
#include<iostream>
#include<cstring>
#include<malloc.h>
using namespace std;
using std::ostream;
class Costmap2D
{
private:
char name[10]; //地图名
unsigned int size_x_;//表示x方向最大距离(x方向像素点个数)
unsigned int size_y_;//表示y方向最大距离(y方向像素点个数)
double resolution_;//分辨率大小
unsigned char* costmap_; //地图数据
unsigned char default_value_;
public:
initMaps();
resetMaps();
//构造函数
Costmap2D (unsigned int size_x_,unsigned int size_y_,double resolution_)
:size_x_(size_x_),size_y_(size_y_),costmap_(NULL),resolution_(resolution_)
{
unsigned int size = size_x_* size_y_;
costmap_= new unsigned char[size];
// create the costmap
}
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution)
{
size_x_ = size_x;
size_y_ = size_y;
resolution_ = resolution;
initMaps(size_x, size_y);
// reset our maps to have no information
resetMaps();
}
void initMaps(unsigned int size_x, unsigned int size_y)
{
// boost::unique_lock<mutex_t> lock(*access_);
delete[] costmap_;
//让指针指向分配的空间
costmap_ = new unsigned char[size_x * size_y];
}
//析构函数
~Costmap2D()
{
delete[] costmap_;
}
Costmap2D operator=(const Costmap2D&);
};
Costmap2D& operator=(const Costmap2D& map)//对等号重载
{
size_x_ = map.size_x_;
size_y_ = map.size_y_;
resolution_ = map.resolution_;
}
ostream& operator<<(ostream& cout,Costmap2D&costmap )//对左移运算符<<重载
{
cout <<"宽="<<coostmap.size_x_<<'\n';
cout <<"长="<<costmap.size_y_<<'\n';
cout <<"分辨率="<<costmap.resolution<<'\n';
cout <<"宽="<<costmap.defaultValue<<'\n';
cout << "Data:\n";
for (unsigned int i = 0; i < map.size_x_(); i++)
{
for (unsigned int j = 0; j < map.size_y_(); j++)
{
cout << std::uppercase << std::setw(2) << std::setfill('0')
<<(map(i, j)) << ' ';
}
cout << '\n';
}
return cout;
}
int main()
{
Costmap2D map = {{1, 2, 3}, {4, 5, 6}, {200, 201, 202}, {255}};
std::cout << map;
return 0;
}
初学求改正
你贴源码吧,这样没法改
这些函数都没定义,你光这么问解决不了问题啊,也不看见你去补充函数。_data的问题也不改
别贴照片,用插入代码块贴源码
25行改为:constmap_ = new unsigned char[size];
31行:多了个分号。
40行:多了个 } ,放到后面去,把重载的成员方法括进去。
全是些基础的错误,你还是回去好好看看书吧,这么多错误也没法一一改,更重要的是我根本看不懂是什么意思。能改的地方都给改了,注释掉的代码是我实在没看懂你想干啥。。
#include<iostream>
#include<iomanip>
using namespace std;
class Costmap2D
{
private:
char name[10]; //地图名
unsigned int size_x_;//表示x方向最大距离(x方向像素点个数)
unsigned int size_y_;//表示y方向最大距离(y方向像素点个数)
double resolution_;//分辨率大小
unsigned char* costmap_; //地图数据
unsigned char default_value_;
public:
//构造函数
Costmap2D(unsigned int size_x_, unsigned int size_y_, double resolution_)
:size_x_(size_x_), size_y_(size_y_), costmap_(NULL), resolution_(resolution_)
{
unsigned int size = size_x_ * size_y_;
costmap_ = new unsigned char[size];
// create the costmap
}
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution)
{
size_x_ = size_x;
size_y_ = size_y;
resolution_ = resolution;
initMaps(size_x, size_y);
// reset our maps to have no information
//resetMaps();
}
void initMaps(unsigned int size_x, unsigned int size_y)
{
delete[] costmap_;
//让指针指向分配的空间
costmap_ = new unsigned char[size_x * size_y];
}
friend ostream& operator<<(ostream& cout, Costmap2D& costmap);
//析构函数
~Costmap2D()
{
delete[] costmap_;
}
Costmap2D& operator=(const Costmap2D& map)//对等号重载
{
size_x_ = map.size_x_;
size_y_ = map.size_y_;
resolution_ = map.resolution_;
return *this;
}
};
ostream& operator<<(ostream& cout, Costmap2D& costmap)//对左移运算符<<重载
{
cout << "宽=" << costmap.size_x_ << '\n';
cout << "长=" << costmap.size_y_ << '\n';
cout << "分辨率=" << costmap.resolution_ << '\n';
cout << "宽=" << costmap.default_value_ << '\n';
cout << "Data:\n";
for (unsigned int i = 0; i < costmap.size_x_; i++)
{
for (unsigned int j = 0; j < costmap.size_y_; j++)
{
//cout << std::uppercase << std::setw(2) << std::setfill('0')
//<< (costmap(i, j)) << ' ';
}
cout << '\n';
}
return cout;
}
int main()
{
//Costmap2D map = { {1, 2, 3}, {4, 5, 6}, {200, 201, 202}, {255} };
//std::cout << map;
return 0;
}