-3

i have this issue but i can't solve it. I only know the InitData function is 100% correct. However i try to find how need to create my classes to make this code work (without do any change to the InitData function)

class Bird
{
public:
    int data[10];
    int color;

    Bird() {
        ZeroMemory(data, 10 * 4);
        color = 0;
    }

    Bird(int* _data, int _color) {
        memcpy(data, _data, 10 * 4);
        color = _color;
    }

    //more code

};

class Animal : Bird
{
public:
    int data[10];

    Animal() {
        ZeroMemory(data, 10 * 4);
    }

    Animal(int* _data) {
        memcpy(data, _data, 10 * 4);
    }

    //more code

};

Animal* InitData(int* _data, bool isBird) {

    if (isBird) {
        Bird* bird = new Bird(_data, 0);
        return bird;
    }
    else {
        Animal* animal = new Animal(_data);
        return animal;
    }

    return nullptr;
}

Maybe has do something with virtual classes?

The compiler gives me error 'E0120 return value type does not match the function type', inside InitData at this line 'return bird;'. I use visual studio 2019, Release(x64), C++17

SOLVED: so i inherit wrong class. Need inherit Animal to Bird. So need change 'class Bird' to 'class Bird : public Animal' and 'class Animal : Bird' to 'class Animal'

Thanks everyone!

mod Zz
  • 49
  • 6
  • 4
    Why does `Animal` derive from `Bird`? It should be the other way around. – David G Apr 11 '21 at 20:44
  • No, your `InitData` function is not 100% correct. It returns a pointer to the child class, and ***NOT*** the parent class. – Sam Varshavchik Apr 11 '21 at 21:02
  • 1
    Your compiler is correct with the error. `InitData` promises to return an `Animal` but you return a `Bird` which is your baseclass. As @0x499602D2 already pointed out your inheritance is the wrong way around, which is also the cause for this error. – Devolus Apr 11 '21 at 21:13
  • @SamVarshavchik and 0x499602D2 sorry my bad, I edited the title – mod Zz Apr 11 '21 at 21:13
  • @Devolus what do you mean? Why need std::vector here? I must not change the InitData function. This isn't my original project but i coded this simple part to make it easier for people help me, the project is way bigger – mod Zz Apr 11 '21 at 21:18
  • The vector is not needed, but your `InitData` is wrong. You can not return a pointer to a baseclass. I assume that your reimplementation of the classes are wrong. – Devolus Apr 11 '21 at 21:20
  • @Devolus so i need to inherit the Animal to Bird? EDIT: i tried it and it still give me the same error – mod Zz Apr 11 '21 at 21:21
  • 1
    Yes. A bird is an animal, but an animal doesn't need to be a bird, because a horse would also be an animal. So it should be `class Animal...; class Bird: public Animal ...` – Devolus Apr 11 '21 at 21:22
  • alright you are right, now it works. Thanks – mod Zz Apr 11 '21 at 21:26

1 Answers1

1

Your inheritance is the wrong way around. Also you don't need to replicate the data in the derived class, because this is what the baseclass is for, so you should call the appropriate constructor, instead of duplicating the code from your baseclass.

class Animal
{
public:
    int data[10];

    Animal()
    {
        ZeroMemory(data, sizeof(int)*10);
    }

    Animal(int* _data)
    {
        memcpy(data, _data, 10 * sizeof(int));
    }

    //more code
};

class Bird : public Animal
{
public:
    int color;

    Bird()
    {
        color = 0;
    }

    Bird(int* _data, int _color)
    : Animal(_data)
    {
        color = _color;
    }

    //more code

};
Devolus
  • 21,661
  • 13
  • 66
  • 113