【发布时间】:2021-05-29 15:40:31
【问题描述】:
我是 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
UserModel.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
【问题讨论】:
-
UserModel.hpp 包含Recipe.hpp,包含Score.hpp,包含Normal_User.hpp,包含UserModel.hpp,循环依赖。
-
我可能想指出,我只是在最近才添加多态性,在添加多态性之前,我通过添加一个虚函数,程序运行良好,我尝试再次将程序改回正常继承,我得到了同样的错误,所以公平地说我不知道这里发生了什么。
-
@S.M.感谢您的评论代码现在可以工作我不得不编辑 Score.hpp 以防止循环依赖我在那里提供了向前减速。
-
这个问题不再符合条件 我不会尝试发布解决方案,因为它对任何人都没有用。版主,请随时关闭问题。