1

私には2つのクラスがPetあり、Person

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_;が、リスト関数を呼び出そうとすると、常にリンクの問題が発生します。私の質問は、別のクラスのオブジェクトを含むクラス内にリストを含める必要がある方法です。

4

1 に答える 1

4

標準では、明示的に記載されている場合を除き、ライブラリ テンプレートで完全な型を使用する必要があります。これは基本的に設計を阻害します (各オブジェクトが値によって他のタイプのリストを維持する場合)。

[スマート] ポインター (コンテナーへのポインターまたはポインターのコンテナー) を使用することで、これを回避できます。

于 2013-04-08T15:42:23.967 に答える