试图传递对象时未申报的标识符
Undeclared identifier when trying to pass in object
我正在尝试通过类型的"餐厅"对象,但是它给了我这个错误 - "未申报的标识符",但是我确保包含名称空间,正确拼写和拼写尽管可能很明显,但我似乎找不到这个问题的来源。
任何人都可以看一看并告诉我发生了什么事?
#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.h
和Header.h
之间具有递归依赖性。restaurant.h
在声明class Restaurant
之前包括Header.h
;Header.h
然后试图包括restaurant.h
来获得餐厅,但由于包括警卫而没有做任何事情。
您可以通过用餐厅的前向销售代替Header.h
中的#include "restaurant.h"
来解决此问题,例如:class Restaurant;
。这也将消除通常需要的循环依赖性。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 声明中类型和对象的相同标识符
- 用户创建的类对象实例化的未申请标识符
- 为什么我的类对象会导致未申报的标识符错误
- 试图传递对象时未申报的标识符
- 指向对象的指针的未声明标识符向量
- 使用对象标识符作为变量名
- C++对象未声明的标识符
- C++ 无法创建具有标识符的对象
- ASN1 对象标识符名称
- 对象标识符、返回值或变量
- c++错误:对全局变量和对象数组使用未声明的标识符
- 比较对象标识符与字符串
- 未声明的标识符,对象向量
- 创建的对象总是未定义,并且在初始化构造函数时期望有标识符
- 哪些容器存储通过不同标识符访问的对象