【问题标题】:Pointer to a Vector of Class - I can't reach the members of the Class指向类向量的指针 - 我无法访问类的成员
【发布时间】:2016-05-03 03:22:53
【问题描述】:

我尝试使用指针创建一个向量(以便所有内容都存储在堆中/堆上)。然后我想用一个类的数组填充向量。我正在考虑通过class[i].member 访问课程...遗憾的是,它不起作用。 如果我在没有矢量的情况下尝试此操作,则可以使用,例如:

tClass *MyClass = new tClass[5]

我在没有特定目的的情况下尝试这样做,只是为了更好地理解 C++。谁能看看我错在哪里?谢谢!

代码如下:

#include "iostream"
#include "vector"
using namespace std;

class tClass
{
private:
   int x = 0;
public:
   int y = 0;
tClass(){cout << "New" << endl;};
~tClass(){}; //do I need to make a delete here?

int main ()
{
   vector<tClass> *MyClass[5];
   MyClass = new vector<tClass>[5];
   cout << MyClass[3].y << endl;
   delete[] MyClass;
}

【问题讨论】:

  • 我很困惑。你想要一个向量数组,还是一个指向向量的指针数组?声明 vector&lt;tClass&gt; *MyClass[5]; 产生后者,但 new vector&lt;tClass&gt;[5] 创建前者。无论哪种方式,MyClass[3] 要么是 vector,要么是指向 vector 的指针,两者都没有名为 y 的成员。
  • 如果你想要一个包含 5 个 tClass 实例的向量(正如 new tClass[5] 所暗示的那样),那么让它成为 vector&lt;tClass&gt; MyClass(5); 你太复杂了。
  • 感谢您对我的“代码”的分析。我现在根据您对语法的建议解决了这个问题。

标签: c++ class pointers vector heap-memory


【解决方案1】:

正如其他人所建议的,如果您只想要一个 tClass 的向量,您可以执行以下操作

vector<tClass> vectorName (5);

然后像这样访问它

vectorName[3].y;

但是,如果您想要一个 tClass 指针向量,您可以像这样初始化和访问它

vector<tClass*> vectorName(5);
vectorName[3]->y;

编辑

这可能对您有更多帮助,这是您的代码,带有注释可帮助您了解问题所在

class tClass

{ 私人的: 诠释 x = 0; 民众: 整数 y = 0; tClass(){ cout

int main()
{
    vector<tClass> *MyClass[5]; //use () to give a vector an initial size, [] is only to access a member
                                //also to have a vector holding pointers, the asterisk needs to be after tClass not before the vector name
    MyClass = new vector<tClass>[5];        
    cout << MyClass[3].y << endl;       //discused above
    delete[] MyClass;                   //only needed if a new is used, however you dont need one here, as it will just go out of scope
}

这是你的代码,但固定为使用指针编译和运行

#include <iostream>
#include <vector>
using namespace std;

class tClass
{
private:
    int x = 0;
public:
    int y = 0;
    tClass(){ cout << "New" << endl; };
};

int main()
{
    vector<tClass*> MyClass(5);
    cout << MyClass[3]->y << endl;
}

请注意,由于类指针的向量未指向任何类,这将产生错误

【讨论】:

  • 亲爱的 Igor 和 Crimson,感谢您的帮助!我可以再问你一点吗?当我在教程书中看到以下代码时,我想到了上面的“代码”:int main () { vector&lt;int&gt; *var; var = new int (5); std::cout &lt;&lt; var[1]; } 我现在试图理解为什么当我用类替换 int 时它不起作用。除此之外,我能否请你给我一个例子,说明vector&lt;tClass*&gt; vectorName(5); vectorName[3]-&gt;y; 如何与“新”一起使用或者这是不可能的(对不起,如果我的问题很无聊,但如果我理解这一点,我会感觉好多了)
  • 我很乐意提供帮助,当从指针访问类成员时,您需要使用指针表示法,而不是使用点,“->”或者您甚至可以执行以下操作如果您更喜欢使用 .符号。在这种特殊情况下,您不需要使用 new。通常,您只想在要删除已知位置的指针时使用 new ,即当它超出范围时。简单地用星号声明一个指针就可以了
  • 亲爱的 Crimson,再次感谢!我查看了我的语法和您的 cmets,我认为以下内容现在可以解决问题:vector&lt;t_class*&gt; MyClass2(5); for (int i=0; i&lt;5; i++) MyClass2[i] = new t_class; MyClass2[2]-&gt;y = 4; delete[]MyClass2。谢谢!
  • 很高兴我能提供帮助,并为我的解决方案添加了更多内容
  • vector&lt;int&gt; *var; var = new int (5); 如果这真的是你的书所说的,那就要求你退钱,买一本更好的书。这段代码没有任何意义,也没有编译的机会。您可能更可能严重错误地引用了书中的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 2018-03-24
  • 2015-07-28
  • 2010-12-20
  • 1970-01-01
  • 2013-06-14
相关资源
最近更新 更多