如何解决{ 多文件项目中的令牌
我是 C++ 的初学者。 我正在编写一个多文件项目,但遇到了一个我不知道如何解决的错误。 我从 ModelUser 的基类继承,该基类具有用户帐户的一般信息,其中包含一个名为 NUser 的类,我在这里基本上使用了多态性。 错误:
In file included from score.hpp:5,from Recipe.hpp:6,from usermodel.hpp:6,from Chef.hpp:1,from Menu.hpp:6,from main.cpp:2:
normal_User.hpp:11:1: error: expected class-name before ‘{’ token
11 | {
| ^
make: *** [Makefile:7: main.o] Error 1
我的代码在遇到 class Nuser : public usermodel
时停止编译,并告诉我在 normal_User.hpp ' } ' 的第 11 行之前需要一个类名。
我四处寻找这个错误的原因,它似乎模棱两可,但我发现我继承的类没有在错误发生的文件点定义。
我不知道为什么我继承的类在编译这个文件时没有包含,我在代码的其他部分使用了这个类,在 main 的开头,我有包含守卫,我有四处测试,看看他们是否导致了问题,但我真的不知道。
然后我试图做一个前向声明,我知道这不会起作用,因为我会得到一个不完整的类型错误。所以我有点卡在这里,这是我的文件,当我收到此错误时,我正在使用来自普通用户的基本用户的多态性。我没有包含某些文件,如果您需要任何其他来源,请随时发表评论。
我应该在这里更改我的代码设计吗,因为我看不出哪里出错了 normal_User.hpp
#include <vector>
#include <iostream>
#include <string>
#include "usermodel.hpp"
#ifndef _norMAL_USER_HPP_
#define _norMAL_USER_HPP_
#include "Recipe.hpp"
class Recipe;
using namespace std;
class Nuser : public usermodel
{
public:
Nuser(vector<string> &commands,int user_size);
void see_recipes(vector<Recipe *> recipes);
private:
};
#endif
用户模型.hpp
#include <vector>
#include <iostream>
#include <string>
#ifndef _USER_MODEL_HPP_
#define _USER_MODEL_HPP_
#include "Recipe.hpp"
using namespace std;
class Recipe;
class usermodel
{
public:
string get_username();
string get_password();
bool get_logged_in();
void user_logout();
void user_login(bool value);
usermodel(vector<string> &commands,int user_size);
virtual void see_recipes(vector<Recipe*> recipes) = 0;
private:
int primary_key;
bool logged_in;
string username;
string password;
};
#endif
制作文件
CC= g++
CXXFLAS = g++ -std=c++11 -g
LDBFLAGS =
output: main.o outerior_functions.o usermodel.o normal_User.o Menu.o Chef.o Recipe.o Exception.o
g++ -std=c++11 -g main.o usermodel.o Exception.o outerior_functions.o Menu.o normal_User.o Chef.o Recipe.o -o output
main.o: outerior_functions.cpp main.cpp outerior_functions.hpp
g++ -std=c++11 -g -c main.cpp -o main.o
usermodel.o: usermodel.cpp usermodel.hpp main_header.hpp
g++ -std=c++11 -g -c usermodel.cpp -o usermodel.o
outerior_functions.o: outerior_functions.cpp outerior_functions.hpp
g++ -std=c++11 -g -c outerior_functions.cpp -o outerior_functions.o
Exception.o: Exception.cpp Exception.hpp main_header.hpp
g++ -std=c++11 -g -c Exception.cpp -o Exception.o
chef.o: chef.cpp chef.hpp main_header.hpp
g++ -std=c++11 -g -c chef.cpp -o chef.o
Recipe.o: Recipe.cpp Recipe.hpp main_header.hpp
g++ -std=c++11 -g -c Recipe.cpp -o Recipe.o
Menu.o: Menu.cpp Menu.hpp main_header.hpp
g++ -std=c++11 -g -c Menu.cpp -o Menu.o
normal_User.o: normal_User.cpp Recipe.hpp normal_User.hpp usermodel.hpp main_header.hpp
g++ -std=c++11 -g -c normal_User.cpp -o normal_User.o
.PHONY: clean
clean:
rm *.o output
Menu.hpp(我的妈妈类)
#include <vector>
#include <string>
#include <iostream>
#ifndef _MENU_HPP_
#define _MENU_HPP_
#include "Chef.hpp"
#include "normal_User.hpp"
#include "usermodel.hpp"
#include "outerior_functions.hpp"
using namespace std;
class Menu
{
public:
void direct_command(vector<string> &commands);
void signup(vector<string> &commands);
void login(vector<string> &commands);
void check_duplication(string &username);
void handle_signup(vector<string> &commands);
void handle_login(vector<string> &commands);
usermodel *user_search(string username);
void logout();
Menu();
private:
vector<usermodel *> users;
vector<Recipe *> recipes;
usermodel *current_user;
};
#endif
解决方法
您在两个文件中都使用了 相同 #define:
#ifndef _NORMAL_USER_HPP_
#define _NORMAL_USER_HPP_
这意味着当您包含一个文件时,不会包含其他文件。
您必须在每个文件中使用不同的 #define 值。或者,如果您有足够新的编译器,请使用 #pragma once
。
嗯 哦,也许您只是在问题中两次包含相同的文件。请编辑您的问题以使其准确无误,并且还请包括对调用的编译行和生成的错误消息的剪切和粘贴,而不是为我们解释它们。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。