samedi 18 juillet 2015

C++: Field has incomplete type

I am trying to implement the strategy design pattern as an exercise. My classes are pretty simple:

1) Fly.cpp

class Fly
{
    public:
        Fly();
        bool fly();
};

class CanFly : public Fly
{
    public:
        bool fly()
        {
            return true;
        }
};

class CantFly : public Fly
{
    public:
        bool fly()
        {
            return false;
        }
};

2) Animal.cpp

class Fly;

class Animal
{
    Fly myFly;

public:
    Animal(Fly f);
    void setFly(Fly f);
    Fly getFly();
};

Animal::Animal(Fly f)
{
    myFly = f;
}

void Animal::setFly(Fly f)
{
    myFly = f;
}

Fly Animal::getFly()
{
    return myFly;
}

3) Dog.cpp

#include <iostream>
using namespace std;

class Animal;

class Dog : public Animal
{
    public:
        Dog(Fly f);
};

Dog::Dog(Fly f)
{
    setFly(f);
    cout << "Dog : " << getFly().fly() << endl;
}

4) Bird.cpp

#include <iostream>
using namespace std;

class Animal;

class Bird : public Animal
{
    public:
        Bird(Fly f);
};

Bird::Bird(Fly f)
{
    setFly(f);
    cout << "Bird : " << getFly().fly() << endl;
}

5) AnimalTest.cpp

#include <iostream>
using namespace std;

class Dog;
class Bird;
class CanFly;
class CantFly;

int main()
{
    Fly f1 = new CanFly();
    Fly f2 = new CantFly();

    Bird b(f1);
    Dog d(f2);

    return 0;
}

The error which I get upon building the code is :

Animal.cpp:5:6: error: field 'myFly' has incomplete type 'Fly'
  Fly myFly;
      ^

Can anyone help me why?

Thanks

Aucun commentaire:

Enregistrer un commentaire