c++的前向声明,里面有一个列表
forward declaration on c++ with a list inside
我有两个类Pet
和Person
Here is the Person.h:
#ifndef PERSON_H
#define PERSON_H
#include <list>
class Pet;
class Person
{
public:
Person();
Person(const char* name);
Person(const Person& orig);
virtual ~Person();
bool adopt(Pet& newPet);
void feedPets();
private:
char* name_;
std::list<Pet> pets_;
};
#endif
这里是pet。h
#ifndef PET_H
#define PET_H
#include <list>
#include "Animal.h"
class Person;
class Pet : public Animal
{
public:
Pet();
Pet(const Pet& orig);
virtual ~Pet();
std::list<Pet> multiply(Pet& pet);
private:
std::string name_;
Person* owner_;
};
#endif
我的问题是:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/list.tcc:129: error: invalid use of undefined type `struct Pet'
Person.h:13: error: forward declaration of `struct Pet'
我固定试图把这个std::list<Pet>* pets_;
,但当我试图调用列表函数总是有一个链接问题。我的问题是如何在包含来自另一个类的对象的类中包含列表。
标准要求,除非有明确说明,否则在库模板中使用完整类型。这基本上抑制了您的设计(其中每个对象通过的值维护另一个类型的列表)。 你可以使用[smart]指针(指向容器的指针或指针的容器)来解决这个问题。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 有没有可能有一个只有ADL才能找到的非好友功能
- 链表,将列表复制到另一个列表
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 在学习数据结构之前对STL有一个了解是好的吗?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 是否有一个标准的算法来查找列表项的并集
- 正在检查排序列表中是否有一个范围内的数字
- C++:图形 ADT 应该有一个顶点列表和一个边列表,或者只是带有指向其他顶点的指针的顶点
- 我可以有一个要实例化的模板列表,而无需事先实例化
- 谁应该有一个迭代器,我的数据类或该类中的实际列表
- 复制链表,每个节点都有随机链接,每个节点都有一个变量,随机指向列表中的另一个节点
- 如果我创建一个列表:<T>:迭代器,有没有办法重载++(增量)运算符?
- c++的前向声明,里面有一个列表
- 我有一个STL向量的列表,我想按照每个向量的第一个元素对它们进行排序
- visualstudio2010-有没有一个列表,说明哪些编译器支持C++标准的哪一部分