试图传递对象时未申报的标识符

Undeclared identifier when trying to pass in object

本文关键字:标识符 对象      更新时间:2023-10-16

我正在尝试通过类型的"餐厅"对象,但是它给了我这个错误 - "未申报的标识符",但是我确保包含名称空间,正确拼写和拼写尽管可能很明显,但我似乎找不到这个问题的来源。

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

#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 &).

我主要通过它,例如:

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

问题是尚未在employee_options中使用Restaurant。这是因为您在restaurant.hHeader.h之间具有递归依赖性。restaurant.h在声明class Restaurant之前包括Header.hHeader.h然后试图包括restaurant.h来获得餐厅,但由于包括警卫而没有做任何事情。

您可以通过用餐厅的前向销售代替Header.h中的#include "restaurant.h"来解决此问题,例如:class Restaurant;。这也将消除通常需要的循环依赖性。