尝试传递对象时未声明的标识符

时间:2019-04-30 21:03:47

标签: c++

我正在尝试传递“餐厅”类型的对象,但是它给了我这个错误–“未声明的标识符”,但是我确保包括名称空间,将其正确拼写,而且似乎找不到源这个问题,尽管可能很明显。

任何人都可以看看并告诉我发生了什么事吗?

#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <string>
#include "menu.h"
#include "pizza.h"

using namespace std;

struct employee {
    int id;
    string password;
    string first_name;
    string last_name;
};

struct hours {
    string day;
    string open_hour;
    string close_hour;
};

#include "restaurant.h"

string get_login_input();
int get_id();
string get_password();
void employee_options(Restaurant &);
//void customer_options();
void print_employee();
int get_employee_option();
void selection(int);

#endif 

问题行为void employee_options(Restaurant &).

我将其传递给main,例如:

int main()
{
    Restaurant r;
    employee_options(r);
// ...

和定义的函数看起来像

void employee_options(Restaurant &r) {
~~~
}

我得到的错误代码是:

In file included from restaurant.h:3:0,
                 from restaurant.cpp:1:
Header.h:28:23: error: variable or field ‘employee_options’ declared void
 void employee_options(Restaurant &);
                       ^
Header.h:28:23: error: ‘Restaurant’ was not declared in this scope
Header.h:28:35: error: expected primary-expression before ‘)’ token
 void employee_options(Restaurant &);

让我知道是否需要更多信息。预先感谢您的帮助

编辑:Restaurant.h看起来像这样:

#ifndef RESTAURANT_H
#define RESTAURANT_H
#include "Header.h"
#include "menu.h"
#include <iostream>
#include <fstream>

using namespace std;

class Restaurant {
private:
    // Members
    menu Menu;
    employee* employees;
    hours* week;
    string name;
    string phone;
    string address;
    int num_employees;
public:
    // Constructor
    Restaurant();
    // copy constructor
    Restaurant(const Restaurant& old_object);
    // Assignment operator overload
    const Restaurant& operator=(const Restaurant& old_object);

    // Accessor functions
    void set_name(string);
    void set_phone(string);
    void set_address(string);

    // Mutator functions
    string get_name() const;
    string get_phone() const;
    string get_address() const;
    void get_employees();

    //need to include constructors, copy constructors, assignment operator overload

    void load_data(); //reads from files to correctly populate menu, employees, hours, etc.
    void fill_r_info();
    void fill_hours(ifstream &file);
    bool login(int id, string password);
    void view_menu();
    void view_hours();
    void view_address();
    void view_phone();
    void search_menu_by_price();
    void search_by_ingredients();
    void place_order(pizza* selection);
    void change_hours();
    void add_to_menu();
    void remove_from_menu();
    void view_orders();
    void remove_orders();

    // Destructor
    ~Restaurant();
};

#endif

1 个答案:

答案 0 :(得分:0)

问题在于Restaurant中使用employee_options时尚未声明。这是因为您在restaurant.hHeader.h之间具有递归依赖性。 restaurant.h在声明Header.h之前包括class RestaurantHeader.h然后尝试包含restaurant.h来获得Restaurant,但由于包含了保护,所以什么也不做。

您可以通过以下方式解决此问题:将#include "restaurant.h"中的Header.h替换为Restaurant的前向声明,例如:class Restaurant;。这还将消除通常需要的循环依赖。