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