Mimi
Mimi

Reputation: 3

How do I return my own struct in c++?

I'm having trouble creating a function that returns my own struct.

Header:

    #ifndef FOOD_H
    #define FOOD_H
    #include <string>

    class Food
    {
    public:
        Food();
        ~Food();
    public:
        struct Fruit {
            std::string name;

        };
        struct Person {
            Fruit favorite;
            Fruit setFavorite(Fruit newFav);
        };

    public:
        Fruit apple;
        Fruit banana;

        Person Fred;
    };
    #endif

CPP:

    #include "Food.h"

    Food::Food()
    {
    }

    Food::~Food()
    {
    }

    Fruit Food::Person::setFavorite(Fruit newFav)
    {
        return newFav;
    }

Main:

    #include "Food.h"
    #include <iostream>

    int main() {
        Food fd;    
        fd.Fred.favorite = fd.Fred.setFavorite(fd.apple);
        std::cout << fd.Fred.favorite.name;
        system("pause");
    }

My errors are:

E0020 identifier "Fruit" is undefined Food.cpp 11

E0147 declaration is incompatible with "Food::Fruit Food::Person::setFavorite(Food::Fruit newFav)" (declared at line 17 of Food.h) Food.cpp 11

How do I fix these and is there a better way to write this code?

Upvotes: 0

Views: 159

Answers (2)

Terrence  Liao
Terrence Liao

Reputation: 7

In addition to the accepted answer, OP's class design could also be improved in my opinion. It seems that OP wants to create a fruit class which should have a is-a relationship with food class. Making it a member of food class doesn't seem right to me. Same thing applies to Person class which should be a separated class instead of being a member of food.

#include <string>

class Food
{
    std::string m_name;
        // other stuffs...
};

class Fruit : public Food
{
    // unique fruit members and functions...
};

class Person
{
    Fruit m_favorite;

public:
    void SetFavorite(Fruit favorite);
};

void Person::SetFavorite(Fruit favorite)
{
    m_favorite = favorite;
}

int main()
{
    Fruit apple;
    Person fred;
    fred.SetFavorite(apple);

    return 0;
}

Upvotes: 1

eerorika
eerorika

Reputation: 238311

identifier "Fruit" is undefined

This error says that there is no definition for Fruit.

You have defined a class Fruit that is nested within Food. Therefore the fully qualified name of the class is Food::Fruit as can be seen from the other error message:

declaration is incompatible with "Food::Fruit Food::Person::setFavorite(Food::Fruit newFav)"
                                  ^^^^^^^^^^^

This error message tells you that the declaration Food::Person::setFavorite(Fruit newFav) is incompatible because that function is supposed to return Food::Fruit rather than Fruit (which is something that doesn't have definition).


Fruit can be used to refer to Food::Fruit within the context of the class Food. The definition of this function is outside of the class, so it is not within the context. It is not until the name of the function (Food::Person::setFavorite) that the context is established. You could use a trailing return type to avoid using the fully qualified type:

auto Food::Person::setFavorite(Fruit newFav) -> Fruit

Upvotes: 5

Related Questions