【问题标题】:How do I return my own struct in c++?如何在 C++ 中返回我自己的结构?
【发布时间】:2018-06-17 01:10:03
【问题描述】:

我在创建返回我自己的结构的函数时遇到问题。

标题:

    #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;
    }

主要:

    #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");
    }

我的错误是:

E0020 标识符“Fruit”未定义 Food.cpp 11

E0147 声明与“Food::Fruit Food::Person::setFavorite(Food::Fruit newFav)”不兼容(在 Food.h 第 17 行声明)Food.cpp 11

我该如何解决这些问题,是否有更好的方法来编写此代码?

【问题讨论】:

  • 错误信息不言自明:Fruit => Food::Fruit
  • 我不知道它告诉我什么与什么不兼容或如何解决它。
  • 将您的代码与错误消息中的声明进行比较。这是编译器所期望的。
  • 好的,知道了。我不确定如何阅读错误。非常感谢。
  • fd.Fred.favorite = fd.Fred.setFavorite(fd.apple); 有点奇怪。 setFavorite 没有达到人们对 setter 函数的期望:它没有设置 Food::Person::favorite。我希望 fd.Fred.setFavorite(fd.apple);favorite = newFav; 并且什么都不返回。

标签: c++ function struct return visual-studio-2017


【解决方案1】:
identifier "Fruit" is undefined

此错误表示Fruit 没有定义。

您定义了一个嵌套在Food 中的类Fruit。因此,该类的完全限定名称是 Food::Fruit,从其他错误消息中可以看出:

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

此错误消息告诉您声明 Food::Person::setFavorite(Fruit newFav) 不兼容,因为该函数应该返回 Food::Fruit 而不是 Fruit(这是没有定义的东西)。


Fruit 可用于在类Food 的上下文中引用Food::Fruit。这个函数的定义在类之外,所以它不在上下文中。直到函数的名称 (Food::Person::setFavorite) 才建立上下文。您可以使用尾随返回类型来避免使用完全限定类型:

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

【讨论】:

    【解决方案2】:

    除了接受的答案,我认为 OP 的类设计也可以改进。似乎 OP 想要创建一个应该与食物类有 is-a 关系的水果类。让它成为食品类的成员对我来说似乎不合适。同样的事情也适用于 Person 类,它应该是一个单独的类而不是 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;
    }
    

    【讨论】:

    • 半相关:这里没有违规,但要小心处理下划线,因为它们可能意味着什么。 What are the rules about using an underscore in a C++ identifier?。如果您从不使用前面的下划线,则不必关心它何时合法。
    • 可以用下划线表示成员吗?
    • 你很好。一个类(或除全局命名空间之外的任何地方)中的单个前下划线是安全的。把它带到课外,你就是在诱惑命运。阅读我第一条评论中的链接。它涵盖了所有规则。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 2021-08-23
    相关资源
    最近更新 更多