简体   繁体   English

C ++。 指针向量和使用向量

[英]C++. Vector of pointers and using the vector

For part of my assignment I need to create a vector of pointers that point to shape objects. 对于我的作业,我需要创建一个指向形状对象的指针向量。 I have a few different classes, the base class Shape, and a few derived classes such as Circle and Rectangle. 我有一些不同的类,基类Shape,以及一些派生类,例如Circle和Rectangle。 In the main I need to construct the vector and then pass the vector to a function that will expand the dimensions of the shape. 首先,我需要构造向量,然后将向量传递给将扩展形状尺寸的函数。 So the function will multiply the radius, the length and the width by a certain number. 因此,该函数会将半径,长度和宽度乘以一定数量。 This is my code so far: 到目前为止,这是我的代码:

vector<Shape*> shapes;
shapes.push_back(circ1);
shapes.push_back(rect1);

with circ1 and rect 1 being objects from the class Circle and Rectangle. circ1和rect 1是Circle和Rectangle类的对象。 I also defined circ1 and rect1 but didnt include the code here. 我还定义了circ1和rect1,但此处未包含代码。

my function is called expandAll(), this is what i tried to do: 我的函数称为expandAll(),这是我尝试做的事情:

void expandAll(vector<Shape*> shapes, int factor)
{
   for (int i = 0; i < shapes.size(); i++)
   {
      shapes[i] = shapes[i] * factor;
   }
}

This is wrong due to the error that popped up. 由于弹出的错误,这是错误的。 To sum up, I need help creating a vector of pointers and using that vector in a function. 综上所述,我需要帮助来创建指针向量并在函数中使用该向量。 Thanks in advance. 提前致谢。

shapes[i] is a pointer. shapes[i]是一个指针。 Which you can't multiply by factor . 你不能乘以factor You need to dereference the pointer, *shapes[i] . 您需要取消引用指针*shapes[i] This assumes that your Shape class (and the derived classes) override the * (multiply, not dereference) operator. 假定您的Shape类(和派生类)覆盖* (乘以,而不是取消引用)运算符。

And when you fix that, assigning it back to shapes[i] is also going to fail. 而当您修复该问题时,将其分配回shapes[i]也将失败。 Likely you'll need to dereference that, too. 您可能也需要取消引用。

There are multiple problems with the incomplete code you've shown in your question. 您在问题中显示的代码不完整存在多个问题。

shapes is a shapes是一个

vector<Shape*>

Therefore, shapes[i] gives you a Shape * . 因此, shapes[i]给您一个Shape * The shown code is equivalent to: 显示的代码等效于:

Shape *p = shapes[i];

shapes[i] = p * factor;

This alternative makes it clear what the problem is. 此替代方法可以清楚地说明问题所在。 Multiplication of a pointer to some object is completely undefined in C++. 在C ++中,完全没有定义指向某个对象的指针的乘法。 There's no such thing. 没有这样的事情。

In C++, you can't multiply a pointer. 在C ++中,您不能将指针相乘。 That's an undefined operation. 那是一个未定义的操作。 It makes no sense to multiply a pointer by anything. 将指针乘以任何东西都没有意义。

What you are most likely trying to do is overload the multiplication operator in your base Shapes class. 您最有可能尝试做的是在基础Shapes类中重载乘法运算符。

However, assuming that your Shapes class has a virtual operator* overload, this still will not work. 但是,假设您的Shapes类具有虚拟operator*重载,则此操作仍将不起作用。 This is because, as you've described, Shapes is a possibly abstract base class, and, as you know, an abstract class is not assignable/copyable (without an overloaded operator for that, which I think is unlikely here), and if your base class is not abstract, the assignment operator will slice it away. 正如您所描述的,这是因为Shapes可能是抽象的基类,并且您知道,抽象类是不可分配/可复制的(没有重载运算符,我认为在这里不太可能),以及您的基类不是抽象的,赋值运算符会将其切掉。 (Not to mention the likely memory leakage). (更不用说可能的内存泄漏)。

So, even if Shapes has an overloaded multiplication operator, (*shapes[i]) = (*shapes[i]) * factor; 因此,即使Shapes具有重载的乘法运算符, (*shapes[i]) = (*shapes[i]) * factor; will still fail. 仍然会失败。

The only way for this approach to work would be if you virtually overload the *= operator, in your abstract base class, and invoke it as 这种方法起作用的唯一方法是在抽象基类中虚拟地*=运算符重载,并以

(*shapes[i]) *= factor;

Your problem is that you probably tried multiplying the object itself : 您的问题是您可能试图乘以对象本身

shapes[i] = shapes[i] * factor;

In spite of that shapes[i] is the pointer to an object, it is a bad idea. 尽管shapes[i]指向对象的指针 ,但这不是一个好主意。

You have to change (multiply) the properties of the object, so use something as 您必须更改(乘以)对象的属性 ,因此请使用以下内容:

shapes[i] -> radius = (shapes[i] -> radius) * factor;

or - more compact - 或者-更紧凑-

shapes[i] -> radius *= factor;

I don't know the names of the properties of your shapes, so instead or radius use appropriate name or names (as rectangles have both width and hight ) and you have decide, too, if the object is of a class Circle or Rectangle . 我不知道形状属性的名称,因此,或者radius使用适当的名称或名称 (因为矩形同时具有widthhight ),并且您也已经确定对象是Circle还是Rectangle类。

You don't give the code, but I assume this is the classical exercice about polymorphism. 您没有给出代码,但是我认为这是关于多态性的经典练习。 So first, assuming you create the circles with Circle circ1, circ2; 因此,首先,假设您使用Circle circ1, circ2;创建圆Circle circ1, circ2; then you must store their adress in the vector, thus: 那么您必须将其地址存储在向量中,因此:

vector<Shape*> shapes;
shapes.push_back( &circ1 );
shapes.push_back( &circ2 );

Then, your function should be: 然后,您的功能应为:

void expandAll( vector<Shape*>& shapes, int factor)
{
   for (int i = 0; i < shapes.size(); i++)
      *shapes[i] = *shapes[i] * factor;
}

But this assumes that your classes all define the * operator, which may not be trivial, if you are a beginner. 但这假设您的类都定义了*运算符,如果您是初学者,这可能并不简单。 And its better to pass it as a reference, so the whole vector doesn't get copied. 并且最好将其作为参考传递,这样就不会复制整个向量。

void expandAll(vector<Shape*>& shapes, int factor)
{
   for (int i = 0; i < shapes.size(); i++)
   {
      *shapes[i] = *shapes[i] * factor;
   }
}

You should try this thing above. 您应该在上面尝试此操作。

shapes[i] = shapes[i] * factor; // Here you're multypling pointers, instead of objects you want

Anyway, I think that still wouldn't work if the operator*() on Shape isn't defined. 无论如何,我认为如果未定义Shape上的operator *(),那仍然行不通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM