I am currently working on a project with several other people. We originally had one header file with class and method declarations and one .cpp file with implementation/main. We decided to try and move the function implementation to the header files so we could all work on different features at the same time without producing as many merge conflicts in git.
When I compile the code, I get "undeclared identifier" errors and others that follow, leading me to the conclusion that something is wrong with the way my headers are set up. The code is long, so I'll try to provide a relevant example:
(Assignment.h and Student.h contain classes in a similar setup)
Instructor.h:
#pragma once
#include "Assignment.h"
#include "Student.h"
#include "Course.h"
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
class Instructor {
private:
std::string instructorName;
std::vector<Course*> courseList;
public:
Course *currentCourse;
Instructor();
Instructor(std::string name);
void setName(std::string name);
std::string getName();
void firstTimeInstructor();
void addCourse();
void addCourse(std::string name);
void removeCourse();
void mainMenu();
void init();
};
... implementation for Instructor methods...
Course.h:
#pragma once
#include "Assignment.h"
#include "Student.h"
#include "Instructor.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Course {
private:
std::string courseName;
std::vector<Student*> studentList;
std::vector<Assignment*> assignmentList;
public:
Course(std::string name);
void setCourseName(std::string name);
std::string getCourseName();
void showCourseOptions();
void addStudent();
void addStudent(std::string first, std::string last);
void addAssignment();
void addAssignment(std::string name, double points);
void print();
void courseMenu();
};
...implementation for Course methods...
First error: instructor.h(17): error C2065: 'Course': undeclared identifier
(Similar ones follow: Student, Assignment, etc.)
I was studying this post for a while, but I wasn't really able to find a definitive answer and I wasn't sure if it even applied to my situation.
I know some of this (such as the using namespace std or even declaring functions in the header) may be bad practice, but we're all a bit new to this so any guidance on the subject would be appreciated.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am getting an error on my private variables Player_player; and Level _level; it is telling me that it is missing the type specifier and im not sure why this is. I have made classes for both Level and Player, so shouldnt I be able to use Player and Level to specify the variable type?
thanks for the help,
mike
//GameSystem.h
#pragma once
#include "Player.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
using namespace std;
class GameSystem
{
public:
GameSystem(string levelFileName);
void playGame();
private:
Level _level;
Player _player;
};
#
//Player.h
#pragma once
#include "GameSystem.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
using namespace std;
class Player
{
public:
Player();
void initPlayer(int level, int health, int attack, int defense, int experience);
//Setters
void setPosition(int x, int y);
//Getters
void getPosition(int &x, int &y);
private:
//Properties
int _level;
int _health;
int _attack;
int _defense;
int _experience;
//Position
int _x;
int _y;
};
Your GameSystem.h file has the line:
#include "Player.h"
Your Player.h file has the line:
#include "GameSystem.h"
This can't be good. And besides the Player class declaration in the header file doesn't use anything from GameSystem.h, so remove the GameSystem.h from the header file (I recommend removing all the #include that don't resolve anything in Player.h header file).
Edit 1:
I changed the header files and made them use Include Guards. I don't get any errors.
Player.hpp:
#ifndef PLAYER_HPP
#define PLAYER_HPP
class Player
{
public:
Player();
void initPlayer(int level, int health, int attack, int defense, int experience);
//Setters
void setPosition(int x, int y);
//Getters
void getPosition(int &x, int &y);
private:
//Properties
int _level;
int _health;
int _attack;
int _defense;
int _experience;
//Position
int _x;
int _y;
};
#endif // PLAYER_HPP
GameSystem.hpp:
#ifndef GSYSTEM_HPP
#define GSYSTEM_HPP
#include "Player.hpp"
#include "Level.hpp"
#include <string>
using namespace std;
class GameSystem
{
public:
GameSystem(string levelFileName);
void playGame();
private:
Level _level;
Player _player;
};
#endif // GSYSTEM_HPP
I changed the filename extensions to help distinguish between C language header files (.h) and C++ language header files (.hpp).
I also simplified the header files by remove unused include files.
You have a dependency problem here, there are two solutions.
First, Player.h isn't actually dependent on GameSystem.h, so don't include GameSystem.h in Player.h.
//Player.h
#pragma once
//Remove GameSystem.h
//#include "GameSystem.h"
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
If, for some reason, you DO need the GameSystem class declaration in the Player class, just do this:
//Player.h
#pragma once
#include "Level.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <vector>
//Declare the class
class GameSystem;
You will need to include the full header file in the .cpp file, but you don't actually need the full class definition to use a class in another classes definition (actually, GameSystem.h doesn't even need to include Player.h, it could just declare but not define the Player class)
#pragma once
#include <string>
#include <list>
#include "Sala.h"
#include "EdicaoDisciplina.h"
#include "Semana.h"
#include "Aluno.h"
using namespace std;
class ActividadeLectiva{
private:
EdicaoDisciplina disciplina;
Semana semana;
list <Sala> salas;
int hora;
string tipo;
string descricao;
list <Aluno> alunos;
public:
ActividadeLectiva();
ActividadeLectiva(EdicaoDisciplina disc, Semana semana, list <Sala> salas, int hora, string tipo, string descricao, list<Aluno> alunos);
ActividadeLectiva(const ActividadeLectiva &e);
~ActividadeLectiva();
EdicaoDisciplina getDisciplina() const;
Semana getSemana() const;
list <Sala> getSalas() const;
int getHora() const;
string getTipo() const;
string getDescricao() const;
list <Aluno> getAlunos() const;
void setDisciplina(const EdicaoDisciplina &e);
void setSemana(const Semana &s);
void setSalas(list <Sala> salas);
void setHora(int hora);
void setTipo(string tipo);
void setDescricao(string descricao);
void setAlunos(list <Aluno> alunos);
};
It keeps giving me tons of error of missing ';' before the identifiers but I swear to god I can't figure where the problem is. Iam kind of tired but still I don't see any error. It will probably be something dumb but could you guys point me out to the right direction?
The problem can be in one of these include files:
#include "Sala.h"
#include "EdicaoDisciplina.h"
#include "Semana.h"
#include "Aluno.h"
It is a good idea to create a .cpp for each .h that is empty asside from including the header so
Sala.h.cpp:
#include "Sala.h"
EdicaoDisciplina.h.cpp:
#include "EdicaoDisciplina.h"
Semana.h:
#include "Semana.h"
Aluno.h:
#include "Aluno.h"
and so on, repeating this for all include files in those include files, etc..
and then check to see if any of the .h.cpp produce a more useful error when you compile them
I have absolutely no idea what's going on. I've been looking up explanations for the weirdness going on here but it seems my situation is in some ways unique. I imagined it was the order in which I include my header files in each of my files, but to no avail, I have not found a combination that seems to be the solution.
The exact error I seem to be getting is "log does not name a type" when declaring LogArray[maxLength].
One of my classes, class logmgmt:
class logmgmt
{
private:
static const int maxLength = 500;
log LogArray[maxLength];
int length;
public:
void fillLogs(int index, int iD, std::string date, double startTime, double endTime);
void displayThisLog(int index);
void setLength(int length);
};
Pre-processor directives within logmgmt.cpp:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "log.h"
#include "Logmgmt.h"
And directives within main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "employee.h"
#include "log.h"
#include "employeemgmt.h"
#include "Logmgmt.h"
Remove using namespace std.
That is polluting the global namespace with lots of symbol names that can cause these conflicts.
In your example, the function std::log becomes log. So it can no longer name a global type.
Ok I know there are millions of variations of this particular problem, and I have tried (desperately) to go through them all and see if they apply, to no avail.
Currently I'm trying to declare a deque in a header file, and the damn thing wont let me due to the error mentioned. The same thing happens to me in a lot of my projects, and I think its just something basic I'm lacking in my knowledge of c++ class syntax.
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include "Card.h"
#include "random.h"
using namespace std;
void createloop();
int get_option();
deque <Card> make_new_deck();
deque <Card> load_new_deck();
int main()
{
createloop();
return 0;
}
I havn't shown the rest of the file for clarities sake, and im pretty confident it isnt the problem. The error appears in Card.h:
Card.h
#ifndef CARD_H
#define CARD_H
class Card
{
public:
Card();
deque<string> param_name_deque;
deque<double> param_value_deque;
virtual ~Card();
protected:
private:
};
#endif // CARD_H
card.cpp
#include "Card.h"
Card::Card()
{
//ctor
}
Card::~Card()
{
//dtor
}
To anyone who can help - thanks in advance! Ill be very happy when I understand whats wrong here!!!
You have to include both std::deque and std::string in your header file card.h
#include <string>
#include <deque>
Meanwhile,
deque<string> param_name_deque;
deque<double> param_value_deque;
should be
std::deque<std::string> param_name_deque;
std::deque<double> param_value_deque;
You need to specify the namespace in card.h when you declare the param_name_deque and param_value_deque:
std::deque<std::string> param_name_deque;
std::deque<double> param_value_deque;
and include the proper headers:
#include <string>
#include <deque>
I would avoid using namespace std, it may seem convenient but it will eventually cause you problems.
I'm pretty sure I've included the qanda class, but when I try to declare a vector that contains it or a class of that type I get an error saying that qanda is undefined. Any idea what the problem might be?
bot_manager_item.h
#pragma once
#include "../bot_packet/bot_packet.h"
#include <vector>
class bot_manager_item;
#include "qanda.h"
#include "bot_manager.h"
class bot_manager_item
{
public:
bot_manager_item(bot_manager* mngr, const char* name, const char* work_dir);
~bot_manager_item();
bool startup();
void cleanup();
void on_push_event(bot_exchange_format f);
bool disable;
private:
void apply_changes();
bot_manager *_mngr;
std::string _name;
std::string _work_dir;
std::string _message;
std::string _message_copy;
std::vector<qanda> games;
qanda test;
char _config_full_path[2600];
};
qanda.h
#ifndef Q_AND_A
#define Q_AND_A
#include "users.h"
#include "..\bot_packet\bot_packet.h"
#include "bot_manager.h"
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <fstream>
class qanda
{
public:
qanda(bot_manager * manager, std::string name, std::string directory);
~qanda(){};
void room_message(std::string username, std::string user_message);
void timer_tick();
private:
// data members
std::string question;
std::string answer;
std::string directory;
std::string command_prefix;
std::string name;
Users users;
std::map <std::string, std::string> questions_and_answers;
int time_per_question; // seconds
int time_between_questions; // seconds
int timer; // milliseconds
bool is_delayed;
bool is_playing;
bot_manager * manager;
// functions
void new_question();
void send_message(std::string msg);
void announce_question();
void load_questions();
};
#endif
Solved: I ended up refactoring the code in such a way as to avoid the use of bot_manager within the qanda class.
I suspect a circular #include problem. Is it possible qanda.h indirectly includes bot_manager_item.h?
It looks like you may be able to reduce header dependencies by using a forward declaration
class bot_manager;
instead of #include "bot_manager.h" in one or both of your posted header files.