| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1039 人关注过本帖
标题:[求助]怎么样把对象内的成员放进容器啊?再加个功能
只看楼主 加入收藏
chenkuanyi
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-4-21
收藏
 问题点数:0 回复次数:3 
[求助]怎么样把对象内的成员放进容器啊?再加个功能

这里是一个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编辑过]

搜索更多相关主题的帖子: 放进 容器 成员 对象 
2007-05-06 08:00
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
楼主注意,push_back是vector的类成员函数,他接受一个参数,该参数是vector元素类型。

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

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


还有楼主你类定义中有很多不必要的东西,复制构造,吸构函数,=操作符

Fight  to win  or  die...
2007-05-06 09:54
chenkuanyi
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2007-4-21
收藏
得分:0 
回复:(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编辑过]

2007-05-06 11:01
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

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


Fight  to win  or  die...
2007-05-06 11:16
快速回复:[求助]怎么样把对象内的成员放进容器啊?再加个功能
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011916 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved