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?