将对象添加到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());更多推荐
发布评论