0

I am a begginer in C++ . I am coding a multi file project and I encountered a bug I don't know how to work around . I am inherting from a base class of ModelUser which has the general info for a user account with a class that Is called NUser ,I am basically using polymorphism here. the error :


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


My code stops compiling when it hits class Nuser : public UserModel and tells me It needs a class name before the in line 11 of Normal_User.hpp ' } ' . I searched around for the cause of this error and it seems to ambigious but what I Found out was that the class I am inheriting from is not defined at the point of the file the error occurs in . I don't know the reason why the class I inherit from is not included at the time of compilation of this file , I have used the class in other parts of the code and at the start of main , I have include guards and I have tested around to see if they are causing the problem but I dont really know .

then I tried to do a forward decleration which I know won't work because I will get an incomplete type error . SO I am kind of stuck here , here is a look at my files , I am using Polymorphism from a base user for a Normal user when I get this error. I havent included certain files ,feel free to comment if you need any other source.

Should i Change my code design here , because I don't see where I have gone wrong 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

makefile

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 (Kind of my mother class )



#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 includes Recipe.hpp, that includes Score.hpp, that includes Normal_User.hpp, that includes UserModel.hpp, the circular dependency. – 273K May 29 '21 at 16:03
  • I might want to note that I added the polymorphism only recently and before adding the polymorphism which I Did by adding a virtual function the program worked fine , I tried changing the program back to normal inheritance again and i got the same error so its fair to say I have no idea whats happening here . – Pouriyatajmehrabi May 29 '21 at 16:03
  • @S.M. Thanks for your comment the code is working now I had to edit Score.hpp to prevent the circular dependency I provided a forward deceleration there . – Pouriyatajmehrabi May 29 '21 at 16:05
  • This question is not eligible anymore I won't try to post a solution as it won't be of use to anyone . Moderators , feel free to close the question. – Pouriyatajmehrabi May 29 '21 at 16:07

1 Answers1

0

You are using the same #define in both your files:

#ifndef _NORMAL_USER_HPP_
#define _NORMAL_USER_HPP_

That means when you include one file, no other file will be included.

You have to use different #define values in each file. Or, use #pragma once if you have a sufficiently new compiler.

Hm Oh maybe you just included the same file twice in your question. Please edit your question to be accurate, and also please include a cut and paste of the compile line that was invoked and the error messages generated, rather than paraphrasing them for us.

MadScientist
  • 92,819
  • 9
  • 109
  • 136