c++ - 在头文件中使用一个类作为另一个类中的值类型

标签 c++ class object header

我在编写自助餐厅模拟项目时遇到了另一个问题。我的想法是我正在编写三个类(学生、小组和托盘)和一个主程序来模拟正在运行的自助餐厅。 托盘 包含某些信息,例如颜色和拿起它的学生 的姓名,以及托盘上食物的总量。在每个托盘学生 配对后,它被插入 堆栈。这就是问题所在。在尝试使用 tray 参数定义 group 函数时,我遇到了各种错误。下面是两个类的代码以及一些错误,再次感谢您的见解:

    class group
{
public:
    //CONSTRUCTOR
    group(string color="nocolor"){group_color=color; group_total=0;}
    //MEMBER FUNCTIONS for group class:
    void set_color(string new_color){group_color=new_color;}
    void add_member(simic_217A::tray new_member){assert(group_color==new_member.color());members.push(new_member);group_total+=new_member.price();}
    //void add_it_up();
    void remove_one() {members.pop();}
    void empty_group(){assert(!members.empty()); while(!members.empty()){members.pop();}} //    NEWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWw

    //CONST MEMBER FUNCTIONS for group class:
    string color() const {return group_color;}
    double  price_tot() const {assert(!members.empty()); return group_total;}
    bool is_empty() const {return members.empty();}
    simic_217A::tray top_item() const {return members.top();}

private:
    double group_total;
    string group_color;
    stack<simic_217A::tray> members;
};

//NONMEMBER FUNCTION for group class:
    std::ostream& operator <<(std::ostream& outs, const group& info);


class tray
{
public:
    //CONSTRUCTOR
    tray(string color="nocolor"){tray_color=color; tray_student_name="noname"; tray_price=0;}
    //MEMBER FUNCTIONS for tray class:
    void pick_up(student nextinline){tray_student_name= nextinline.get_name(); tray_price= nextinline.price(); nextinline.set_group(tray_color) ;}
    string set_color(string color){tray_color=color;}
    void drop_tray(){tray_student_name="noname"; tray_price=0;} 
    //CONST MEMBER FUNCTION for tray class:
    string tray_name() const {return tray_student_name;}
    double tray_price_func() const {return tray_price;}
    string color () const {return tray_color;}
private:
    string tray_color;
    string tray_student_name;
    double   tray_price;
};
  }

以下是我在尝试编译时遇到的几个错误:

    error C2039: 'tray' : is not a member of 'simic_217A'

    error C2061: syntax error : identifier 'tray'

    error C2146: syntax error : missing ';' before identifier 'top_item'

最佳答案

tray 的定义需要出现在任何依赖于它的类之前,例如group。否则编译器将不知道托盘是什么。

关于c++ - 在头文件中使用一个类作为另一个类中的值类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10544435/

相关文章:

c++ - 缺少类型说明符,编译器将 Class* 更改为 int*

javascript - 将对象数组中的值插入到一组元素中

java - 序列化对象数组以通过套接字发送

javascript - 使用 DefineProperty 进行获取/设置

c++ - 用于定义数字的数字常量之前的预期 unqualified-id

c++ - 程序跳过第二个 cin

c++ - 如何在 C++ 中调试 WM_PAINT 中的代码?

c++ - 使用 FastDelegate 的段错误

java - 在单独的类 Java 中调用时 ArrayList 为空

java - 隐藏一个类对其他类的可见性