在C ++中将对象添加到对象数组 [英] Add Object to Array of Objects in C++

查看:253
本文介绍了在C ++中将对象添加到对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个对象数组,要向其中添加对象,我尝试以下操作:

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 是整数变量,Shape是类, ShapeList是对象数组.但是编译器以这种方式给出错误,不允许使用!=运算符.那么我该如何实现呢?

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?

推荐答案

我认为您需要更改容器"声明:

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;
        }
   }
}

并以这种方式调用addShape:

and call addShape this way:

addShape(new Shape());

这篇关于在C ++中将对象添加到对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆