Visual Studio 2012 missing ; - c++

#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

Related

Issues with function implementation in multiple header files

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.

Redefinition error: Different .h files using the same class name

I've created 2 header files. ListA.h and ListN.h
They both make their own use their own unique class List. When I compile my program (even though they have no way of knowing the other exists, it says the following error)
Im pretty sure it shouldnt be a redefinition, but it obviously is. Any help is appreciated.
ListA.h
#ifndef __LISTA_H_
#define __LISTA_H_
#include <iostream>
using namespace std;
class List{
public:
List(int = 0);
List(const List&);
~List();
};
#endif
ListN.h
#ifndef __LISTN_H_
#define __LISTN_H_
#include <iostream>
using namespace std;
class List{
public:
List(int = 10);
List(const List&);
~List();
};
#endif
ListA.cpp
#include "ListA.h"
using namespace std;
List::List(int mySize)
{
//...
}
ListN.cpp
#include "ListN.h"
#include <iostream>
using namespace std;
List::List(int size)
{
//...
}
Main
#include <iostream>
#include "ListN.h"
using namespace std;
int main()
{
List myList;
return 0;
}
Both cpp files are being compiled by the compiler. Thus, when the linker goes to link the files together, it gets confused, since there are multiple List classes.
To fix this, you could use namespaces, or you cold not expose at least one of the List classes.
Alternatively, if the idea was to be able to include ListN.h vs ListA.h for configuration purposes, this is the wrong way to do so. Either you should have a #define parameter for the header, or you should find some other way, such as through #ifdef. For example (I'm not 100% sure this would compile, but you get the idea):
List.h
#ifndef __LIST_H_
#define __LIST_H_
#ifndef LIST_PARAM
#define LIST_PARAM 0
#endif
#include <iostream>
using namespace std;
class List{
public:
List(int = LIST_PARAM);
List(const List&);
~List();
};
#endif
main.cpp
#include <iostream>
#define LIST_PARAM 10
#include "List.h"
using namespace std;
int main()
{
List myList;
return 0;
}
I personally don't like this method; it is much better to just pass the value in to the constructor:
int main()
{
List myList{ 10 };
return 0;
}
When linker trying to link find the definition / symbol for List, it does found in two different obj file and hence linker givers error. In visual studio error number : LNK2005
To solve this error, either:
To fix, add /FORCE:MULTIPLE to the linker command line options
Add the classes in two different namespaces which will avoid this error.
ListN.h
#ifndef __LIST_H_
#define __LIST_H_
#include <iostream>
using namespace std;
namespace ListN
{
class List{
public:
List(int = 10);
List(const List&);
};
}
#endif
ListN.cpp
#include "ListN.h"
#include <iostream>
using namespace std;
namespace ListN
{
List::List(int size)
{
//...
}
}
Main.cpp
#include <iostream>
#include "ListN.h"
int main()
{
ListN::List myList;
return 0;
}

C++ does not support default-int [closed]

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)

error C2511 : overloaded member function not found in Class

This is the error im recieving.
Error 4 error C2511: 'Vector<T> Menus::sortListBy(sortType,Vector<T>)'
: overloaded member function not found in 'Menus' z:\documents\visual
studio 2010\projects\oo_cw\oo_cw\menus.cpp 410 1 OO_CW
I believe this is something to do with me trying to use an enum that is included in a header but doesnt seem to be carried over to the other classes.
Here are the 2 headers involved and the function im struggling with::
Menus.cpp:
Vector<connections> Menus::sortListBy(sortType sortKey,Vector<connections> sortList){}
Menus.h
#pragma once
#include "std_lib_facilities.h"
#include "Airport.h"
class Journey;
#include <string>
typedef enum {BACK,FORWARD,INVALID,OPTIONS} result;
typedef enum {BOOK,VIEW,EXIT} firstChoice;
class Menus
{
public:
Menus(void);
~Menus(void);
firstChoice firstMenu();
Airport bookingMenuFirst(Vector<Airport>);
Airport bookingMenuSecond(Vector<connections>,Vector<Airport>);
airlines bookingMenuThird(Airport,Airport,Journey&);
string bookingMenuDate();
bool showReciept(string,string,string,string,double,double,double);
string showRecieptNames();
void readReciept(string);
void optionMenu(Journey);
Vector<connections> sortListBy(sortType,Vector<connections>);
};
Journey.h
#pragma once
#include "std_lib_facilities.h"
#include "Airport.h"
#include <string>
#include "Menus.h"
enum sortType {PRICE,TIME} ;
class Journey
{
public:
Journey(void);
~Journey(void);
//accessors
Airport getStart();
Airport getEnd();
string getDate();
airlines getAirline();
string getStringAirline();
double getTime();
double getPrice();
sortType getSort();
//modifiers
void setStart(Airport);
void setEnd(Airport);
void setPrice(double);
void setTime(double);
void setAirline(airlines);
void setDate(string);
void saveBooking();
void setSort(sortType);
private:
Airport startAirport;
Airport endAirport;
double price;
double time;
string date;
airlines airline;
Vector<string> splitBy(string,string);
sortType sortingBy;
};
Menus.cpp header statements
#include "Menus.h"
#include "std_lib_facilities.h"
#include "Airport.h"
#include "Journey.h"
#include <string>
using namespace std;
The enum sortType is defined in Journey.h
however, is not visible in menu.h and you are using enum sortType as input argument in the declaration of member function Menus::sortListBy(sortType,Vector<connections>); in the definition of the Menus class.
In Menus.h remove the forward declaration of class Journey; and replace it with #include "Journey.h".
Remove #include "Menus.h" in Journey.h.
There should be no problem, since you don't have circular dependency issues between Journey and Menus.

Classes as parameters error

In Weapon.h, when I try and take a class 'Entity*' as a parameter, it says "Syntax error: identifier 'Entity'" when I compile. Additionally when I roll over the text 'target', Visual C++ Express 2010 gives me the text " *target". The Entity class is fine and I'm pretty sure it's included correctly.
(I won't post Player.h as it's unnecessary - see Library.h - but it has a header guard and includes Entity.h)
Library.h:
#ifndef _LIBRARY_
#define _LIBRARY_
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <map>
#include <exception>
#include <sstream>
//file includes
#include "Globals.h"
#include "Player.h"
#include "Exception.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
//prototypes that require "Library.h"
bool Poglathon(std::vector<std::string>& text,Player *player);
bool PoglathonTown(std::vector<std::string>& text,Player *player);
std::map<std::string,Weapon*> init_weapons(void);
std::map<std::string,Armour*> init_armour(void);
std::map<std::string,Consumable*> init_consumables(void);
#endif //__LIBRARY__
Weapon.h:
#ifndef _WEAPON_H_
#define _WEAPON_H_
#include "Shopable.h"
class Weapon : public Shopable{
private:
int Damage;
public:
Weapon(int c,int d,std::string n) : Shopable(c,n),Damage(d){}
std::string getDesc() const{
return getName()+"\t"+tostring(Damage)+"\t"+tostring(Cost);
}
int getDamage() const{return Damage;}
int DamageTarget(Entity* target){
int DamageDealt = 0;
//do damage algorithm things here
return DamageDealt;
}
};
#endif
Shopable.h:
#ifndef _SHOPABLE_H_
#define _SHOPABLE_H_
#include "Library.h"
class Shopable{
protected:
std::string Name;
int Cost;
std::string Description;
public:
Shopable(int c, std::string n):Cost(c),Name(n){}
std::string getName() const{return Name;}
int getCost() const {return Cost;}
virtual std::string getDesc() const = 0;
};
#endif
Entity.h:
#ifndef _ENTITY_
#define _ENTITY_
#include "Library.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
class Entity{
public:
void printStats() const;
void heal(double health);
std::string name;
protected:
//player stats
double str; //strength
double wis; //wisdom
double ref; //reflex
double hp; //health points
double maxHp; //maximum health points
double i; //initiative
double inte; //intelligence
double c; //courage
int gold; //gold
int xp; //experience
int ap; //armour points
int wd; //weapon damage
int lvl; //level
int sp; //skill points
Weapon* weapon;//weapon
Armour* cArmour;//current armour
};
#endif
In C++, classes must be declared before they are referenced. You are #include-ing Weapon.h in Entity.h, but at that point, the compiler doesn't know about the existence of class Entity.
You will either need to change the order in which things are declared, or add a forward declaration "above" class Weapon. It can simply be:
class Entity;
That tells the compiler that there is such a name as Entity. However, it doesn't tell it anything about what members it has, so you can't actually do anything with it, other than declare variables of Entity * and Entity &, and pass them around.
Your headers include each other because your classes refer to each other. (But your compiler doesn't suffer from a stackoverflow because of your include guards - that's a good thing!)
You should arrange your header files hierarchically, ie there are files at the 'top' which #include nothing and files 'below' which include some of the top ones and so-on down the hierarchy. But at no point should there be 'loops'.
In order to break your loops in your code, any classes that refer to each other should forward declare any mutual dependencies and only refer to dependency names and not their members.
e.g.
Entity.h
class Weapon;
class Entity{
...
Weapon* weapon;
};
Weapon.h
class Entity;
class Weapon{
...
int DamageTarget(Entity* target);
};
Notice how Weapon.h only refers to Entity*.
You will need to define int Weapon::DamageTarget(Entity* target) in Weapon.cpp
#include <entity.h>
Or forward-declare only in the header
class Entity;
This makes compilation a bit faster (you still need to include it to use in the implementation).
Weapon.h doesn't #include Entity.h, or anything that recursively includes it. Therefore, it doesn't know about the class Entity.