将对象指针向量传递给类(C++)

将对象指针向量传递给类(C++),c++,class,object,vector,containers,C++,Class,Object,Vector,Containers,我有一个对象指针向量 std::vector<myObject *> listofObjects; 我做错了什么?我只想将一个向量传递给NeedsObjects类。您没有告诉编译器myObject是什么,因此它不知道如何创建std::vector。使用.h文件添加引用或在此翻译单元中定义myObject 或者 #include "myObject.h" class NeedsObjects { public: NeedsObjects(std::vector<m

我有一个对象指针向量

std::vector<myObject *> listofObjects;

我做错了什么?我只想将一个向量传递给NeedsObjects类。

您没有告诉编译器myObject是什么,因此它不知道如何创建std::vector。使用.h文件添加引用或在此翻译单元中定义myObject

或者

#include "myObject.h"

class NeedsObjects 
{
 public:
    NeedsObjects(std::vector<myObject *> &listofObjects)
 private:
    std::vector<myObject *> &listofObjects;
};

您没有告诉编译器myObject是什么,因此它不知道如何创建std::vector。使用.h文件添加引用或在此翻译单元中定义myObject

或者

#include "myObject.h"

class NeedsObjects 
{
 public:
    NeedsObjects(std::vector<myObject *> &listofObjects)
 private:
    std::vector<myObject *> &listofObjects;
};

正如我看到的,代码中没有任何myOpbject类型的声明

您基本上有两种选择:

完全声明myObject的Include标头

#include "myObject.h" // ... or something near to this.
b让我们认为我的对象是类。您在这里提供的代码(至少声明部分)实际上不需要知道myObject的大小,所以您可以将myObject声明为类,并在其他地方声明

class myObject;

正如我看到的,代码中没有任何myOpbject类型的声明

您基本上有两种选择:

完全声明myObject的Include标头

#include "myObject.h" // ... or something near to this.
b让我们认为我的对象是类。您在这里提供的代码(至少声明部分)实际上不需要知道myObject的大小,所以您可以将myObject声明为类,并在其他地方声明

class myObject;

您使用指向该对象的指针,因此不必定义完整的对象结构,只需在使用之前在此文件中声明即可:

class myObject; // pre declaration, no need to know the size of the class
class NeedsObjects 
{
 public:
    NeedsObjects(std::vector<myObject *> &listofObjects)
 private:
    std::vector<myObject *> &listofObjects;
};

您使用指向该对象的指针,因此不必定义完整的对象结构,只需在使用之前在此文件中声明即可:

class myObject; // pre declaration, no need to know the size of the class
class NeedsObjects 
{
 public:
    NeedsObjects(std::vector<myObject *> &listofObjects)
 private:
    std::vector<myObject *> &listofObjects;
};

您需要在NeedsObjects之前声明myObject。您需要在NeedsObjects之前声明myObject。@WernerHenze它是指针的容器。当然这里允许输入不完整的字符。@WernerHenze是的,你必须等到OP进行编辑,然后才能删除它:这完全解决了我的问题,非常感谢你,现在你可以再给我一次投票@它是指针的容器。当然这里允许输入不完整的字符。@WernerHenze是的,你必须等到OP进行编辑,然后才能删除它:这完全解决了我的问题,非常感谢你,现在你可以再给我一次投票;添加头文件无效,但部分声明有效!在哪个头文件中声明myObject类?添加头文件不起作用,但部分声明起作用!在哪个标题中声明myObject类?