将对象添加到C ++中的对象数组(Add Object to Array of Objects in C++)

编程入门 行业动态 更新时间:2024-10-26 13:21:55
对象添加到C ++中的对象数组(Add Object to Array of Objects in C++)

有一个对象数组,并添加对象,我尝试以下方法:

Shape ShapeList[30]; void addShape(Shape s) { for(int i=0; i<30;i++) { if(ShapeList[i] != '\0') { i++;} else { ShapeList[i]=s; numShapes++; break; } } }

numShapes是一个整型变量,Shape是类, ShapeList是对象数组 。 但是编译器以这种方式给出错误,即不允许使用!=运算符。 那么我如何实现这个?

There is an array of objects and to add object to it i tries the following:

Shape ShapeList[30]; void addShape(Shape s) { for(int i=0; i<30;i++) { if(ShapeList[i] != '\0') { i++;} else { ShapeList[i]=s; numShapes++; break; } } }

numShapes is an integer variable, Shape is the class and ShapeList is the array of objects. But the compiler gives an error in this way that != operator is not allowed. So how can i implement this?

最满意答案

我认为你需要改变你的'容器'声明:

Shape *ShapeList[30]; void addShape(Shape *s) { for(int i=0; i<30;i++) { if(ShapeList[i]) { i++;} else { ShapeList[i]=s; numShapes++; break; } } }

并以这种方式调用addShape:

addShape(new Shape());

I think you need to change your 'container' declaration:

Shape *ShapeList[30]; void addShape(Shape *s) { for(int i=0; i<30;i++) { if(ShapeList[i]) { i++;} else { ShapeList[i]=s; numShapes++; break; } } }

and call addShape this way:

addShape(new Shape());

更多推荐

本文发布于:2023-07-17 04:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   数组   Add   Objects   Array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!