注册 登录
编程论坛 C++教室

[求助]怎么样把对象内的成员放进容器啊?再加个功能

chenkuanyi 发布于 2007-05-06 08:00, 1043 次点击

这里是一个shape类,要用一个容器,把它内面的成员都放进去,然后好实现一些功能啊!!

#include <iostream>
#include <vector>
using namespace std;

class shape
{
public:
int x_pos;
int y_pos;
int color;
public:
shape() : x_pos(0), y_pos(0), color(1) {}
shape(int x, int y, int c = 1) : x_pos(x), y_pos(y), color(c) {}
shape(const shape& s) : x_pos(s.x_pos), y_pos(s.y_pos), color(s.color) {}
~shape() {}
shape& operator=(const shape& s)
{
x_pos = s.x_pos;
y_pos = s.y_pos;
color = s.color;
return *this;
}

int get_x_pos() { return x_pos; }
int get_y_pos() { return y_pos; }
int get_color() { return color; }

void set_x_pos(int x) { x_pos = x; }
void set_y_pos(int y) { y_pos = y; }
void set_color(int c) { color = c; }

virtual void DrawShape() {}

friend ostream& operator<<(ostream& os, const shape& s);
friend istream& operator>>(istream& is, shape& s);
};

ostream& operator<<(ostream& os, const shape& s)
{
os << "shape: (" << s.x_pos << "," << s.y_pos <<"," << s.color << ")";
return os;
}
istream& operator>>(istream& is, shape& s)
{
is>>s.x_pos>>s.y_pos>>s.color;
return is;
}

void main()
{
vector<shape> shape_vector(10);//设定容器大小

shape_vector.push_back(10,1,20);//把成员放进容器
shape_vector.push_back(10,10,2);
shape_vector.push_back(1,10,20);
shape_vector.push_back(10,11,20);
shape_vector.push_back(19,1,20);
shape_vector.push_back(18,10,2);
shape_vector.push_back(1,10,20);
shape_vector.push_back(12,11,20);
shape_vector.push_back(1,18,20);
shape_vector.push_back(12,11,21);


vector<shape>::iterator shape_iterator;

//输出成员
for(shape_iterator=shape_vector.begin();shape_iterator!=shape_vector.end();shape_iterator++)
{
cout<<*shape_iterator<<endl;
}
}


错误:
45.cpp(53) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(54) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(55) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(56) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(57) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(58) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(59) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(60) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(61) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]
45.cpp(62) : error C2660: “std::vector<_Ty>::push_back”: 函数不接受 3 个参数
with
[
_Ty=shape
]

[此贴子已经被作者于2007-5-6 11:04:48编辑过]

3 回复
#2
aipb20072007-05-06 09:54
楼主注意,push_back是vector的类成员函数,他接受一个参数,该参数是vector元素类型。

你直接给他传三个参数是不能实现类类型转换的。必须显式指定。可以这样:

shape_vector.push_back(shape(10,1,20));//把成员放进容器


还有楼主你类定义中有很多不必要的东西,复制构造,吸构函数,=操作符
#3
chenkuanyi2007-05-06 11:01
回复:(aipb2007)楼主注意,push_back是vector的类成...

谢谢啊!

我还想在里面加入一个查找功能,如查找x_pos为19的shape,再输出这个shape,
这是这样加的,但是是错的,请改正一下啊

for(shape_iterator=shape_vector.begin();shape_iterator!=shape_vector.end();shape_iterator++)
{
if(*shape_iterator.x_pos=19)//这里有错
cout<< *shape_iterator<<endl;
else
cout<<"NOT FOUND"<<endl;
}

48.cpp(58) : error C2039: “x_pos”: 不是“std::_Vector_iterator<_Ty,_Alloc>”的
成员
with
[
_Ty=shape,
_Alloc=std::allocator<shape>
]

[此贴子已经被作者于2007-5-6 11:02:08编辑过]

#4
aipb20072007-05-06 11:16

优先级问题,加括号 if((*shape_iterator).x_pos=19)//这里有错
最好用->操作符!if(shape_iterator->x_pos=19)//这里有错

1