高手请看

03server 2003-09-11 02:55:43
请看下面这个程序:
struct Point{
int x_axis,y_axis;
vector<int> slope;
void set_value(int x,int y) { x_axis = x; y_axis = y;}
}

int main(){
vector< Point> point_set;
int x,y,i(0);
cin >> x >> y;
point_set[i++].set_value(x,y);
}

程序在vc++6.0(windows xp professional)下能够编译通过,可是在运行是会出错。具体错误为:我在输入如12 23的坐标后,enter回车,系统会跳出xp的那个
“经典”错误:1165.exe has encountered a problem and needs to close. We are sorry for the inconvenience. If you were in the middle of something ,the information you were working on might be lost....

其中1165为我这个程序的名字。
我曾试着在struct Point 中放入一个constructor: Point(int x, int y) :
x_axis = x, y_axis = y{}
然后在main中这样:

int main(){
vector< Point> point_set;
int x,y,i(0);
cin >> x >> y;
Point new_node = new Point(x,y);
point_set[i++].push_back(new_node);
}

这样更改后编译时期就会报错。
请问如何在一个vector<somekind_class > T 中添加一个somekind_class 的对象?
此外,vector中的somekind_class中是否允许有vector定义呢,如上例子中的vector<int> slope;

先行谢过!

...全文
32 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
03server 2003-09-13
  • 打赏
  • 举报
回复
谢谢sevecol,谢谢tranum。问题已解决
l1ul1u 2003-09-11
  • 打赏
  • 举报
回复
sevecol(sevecol.blogone.net) 说的很对
sevecol 2003-09-11
  • 打赏
  • 举报
回复
你的point_set是一个vector但是你没有放置任何的Point进取,你如何能取出来执行set_value??

你下面的代码有几处错误
new Point(x,y)得到的是Point*,不是Point
所以你在push_back的时候需要*new_node;
而point_set[i++]得到的是Point对象,应该直接使用point_set.push_back(*new_node);

//
struct Point{
int x_axis,y_axis;
vector<int> slope;
Point(int x,int y):x_axis(x),y_axis(y){}
void set_value(int x,int y) { x_axis = x; y_axis = y;}
};

int main(){
vector< Point> point_set;

int x,y,i(0);
cin >> x >> y;
Point *new_node = new Point(x,y);
point_set.push_back(*new_node);
}
Tranum 2003-09-11
  • 打赏
  • 举报
回复
老兄,你生成了一个Point的向量,但是你并没有向里面压入元素啊,调用set_value()的时候那还是个空向量呐,真么会不出错!!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧