将对象添加到 C++ 中的对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9375125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Object to Array of Objects in C++
提问by Rog Matthews
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] != 'Shape *ShapeList[30];
void addShape(Shape *s)
{
for(int i=0; i<30;i++)
{
if(ShapeList[i])
{ i++;}
else
{
ShapeList[i]=s;
numShapes++;
break;
}
}
}
')
{ i++;}
else
{
ShapeList[i]=s;
numShapes++;
break;
}
}
}
numShapesis 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?
numShapes是一个整数变量,Shape 是类,ShapeList 是对象数组。但是编译器以这种方式给出错误,!=
不允许操作符。那么我该如何实现呢?
采纳答案by CapelliC
I think you need to change your 'container' declaration:
我认为您需要更改“容器”声明:
addShape(new Shape());
and call addShape this way:
并以这种方式调用 addShape:
class Shape
{
//your implementation
//
public:
bool operator != (char x) const
{
// comparison logic
}
};
回答by Luchian Grigore
ShapeList[i]
returns an object of type Shape
. In that case, you can overload operator != (char)
.
ShapeList[i]
返回类型为 的对象Shape
。在这种情况下,您可以重载operator != (char)
.
if(ShapeList[i] != 'if(ShapeList[i] != 'Shape * ShapeList[30];
numShapes=0;
void addShape(Shape* s)
{
if( i>=30)
return;
ShapeList[numShapes++]=s; // provided you need to insert at end
}
')
{ continue;}
')
{ i++;}
Also, I believe you have a mistake here:
另外,我相信你在这里有一个错误:
##代码##I assume you want to skip this case, but you already increment i
in the for
loop. You probably want:
我假设您想跳过这种情况,但您已经i
在for
循环中增加了。你可能想要:
As others have pointed out, you should use a std::vector
instead of a raw array. I initially assumed ShapeList
was a wrapper over a std
container.
正如其他人指出的那样,您应该使用 astd::vector
而不是原始数组。我最初认为ShapeList
是std
容器上的包装器。
回答by Rohit Vipin Mathews
You can't use \0
because it's an array, not a string.
您不能使用,\0
因为它是一个数组,而不是一个字符串。
storing the whole object as such is an overhead with memory. pointers are a better choice unless you have local variables going out of scope problems. and if STL and Vectors is not beyond your scope of the project you are on to try using it. in which you can use pushback() or pushfront()
这样存储整个对象是内存开销。除非局部变量超出范围问题,否则指针是更好的选择。如果 STL 和 Vectors 不超出您的项目范围,您可以尝试使用它。您可以在其中使用 pushback() 或 pushfront()
回答by Some programmer dude
Unless you have a conversion operator in the Shape
class to convert it to a character, or a not-equal comparison operator that takes a character as argument, you can not compare a Shape
object to a char
like you do.
除非您在Shape
类中有一个转换运算符将其转换为字符,或者有一个将字符作为参数的不等比较运算符,否则您无法像这样将Shape
对象与 a进行比较char
。
You should be using a std::vector
or std::array
and use the at
member function to see if entry exists or not.
您应该使用std::vector
orstd::array
并使用at
成员函数来查看条目是否存在。
回答by Willem Hengeveld
You did not specify how ShapeList
is declared.
您没有指定如何ShapeList
声明。
With the != operator you compare it to the character NUL, while 4 lines below you assign it a Shape object.
使用 != 运算符将其与字符 NUL 进行比较,而在下面的 4 行中,您为其分配一个 Shape 对象。
What i think you are trying to achieve, is: find an empty slot in the array of pointers to Shape, and store the Shape there.
我认为您要实现的是:在指向 Shape 的指针数组中找到一个空槽,并将 Shape 存储在那里。
But probably better is to use either a std::vector, or std::list, and push_back your shape.
但可能更好的是使用 std::vector 或 std::list,然后 push_back 您的形状。
Another thing you have to ask yourself: do i want to store copies of my Shape object, or pointers?
您必须问自己的另一件事:我想存储 Shape 对象的副本还是指针?