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)
Related
I have the following issue thrown by the compiler:
include/FlowChannel.h:14:21: error: ‘LatticeCell’ was not declared in this scope
std::vector grid;
when having these 3 header files (LatticeCell.h, FlowChannel.h and Utilities.h) and 2 cpp files including them(lbm.cpp and Utilities.cpp):
LatticeCell.h
#ifndef LATTICECELL_H
#define LATTICECELL_H
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
/* Single cell */
using namespace std;
class LatticeCell{
private:
std::vector<double> matrix = {0,0,0,0,0,0,0,0,0};
unsigned int type; //fluid, no-slip, velocity or density
public:
//Constructor
LatticeCell(unsigned int inType){
type = inType;
}
};
#endif
FlowChannel.h
#ifndef FLOWCHANNEL_H
#define FLOWCHANNEL_H
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include "LatticeCell.h"
using namespace std;
class FlowChannel{
private:
std::vector<LatticeCell> grid; //ERROR LINE
unsigned int dimX = -1;
unsigned int dimY = -1;
public:
FlowChannel(unsigned int nx, unsigned int ny){
dimX = nx+2;
dimY = ny+2;
unsigned int gridSize = dimX*dimY;
grid.reserve(gridSize);
initGrid(/*TODO Params*/);
}
};
#endif
lbm.cpp
#include <string>
#include <vector>
#include "LatticeCell.h"
#include "FlowChannel.h"
#include "Utilities.h"
int main(int argc, char** argv){
printsomething();
return 0;
}
Utilities.cpp
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
void printsomething(){
cout << "something" << std::endl;
}
double calcRelaxationTime(unsigned int ny , double reynolds, double uin){
return 3.0 * (uin * ny / reynolds) - 0.5;
}
Utilities.h
#ifndef UTILITIES_H
#define UTILITIES_H
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <vector>
#include <cmath>
void printsomething();
#endif
Further my compiler flags are:
-Wall -std=c++17 -pedantic
For some reason I can't figure out, why LatticeCell wouldnt be a declared class in FlowChannel, due to it being included. Do you guys know whats wrong?
Edit: I added lbm.cpp, Utilities.cpp and Utilities.h so you guys see the full scope of the problem
You should check if the files are in the same directory.
I copy and paste your code in VS 2019 and it work for me,
here are the pictures
FlowChannel LatticeCell
It seems, that deleting #include 'LatticeCell.h' everywhere but in FlowChannel.h. I dont get the error 100% to be honest, as this wouldn't execatly cause an include loop that would induce such an error, but it works.
I keep getting this error and I am not sure why. I get the error posted in the question title. If I take the semicolon out of the class line I get expected semicolon after class.
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctime>
#ifndef die_h
#define die_h
using namespace std;
class Dice;
{
private:
int sides;
int value;
public:
Dice(int=6);
void cast();
int getSides();
int getvalue();
return 0;
}
#endif
I've got three classes in a project I'm working on called Pixel, custFrame, and FrameHolder.
My custFrame class header is like so:
#pragma once
#include "stdafx.h"
#include <gl/gl.h>
#include "PreviewWindow.h"
#include <iostream>
#include <sstream>
#include <vector>
#include "FrameHolder.h"
#include "Pixel.h"
#ifndef CUSTFRAME_H
#define CUSTFRAME_H
class custFrame
{
public:
custFrame();
void addPixel(Pixel pix);
void setWidth(int width);
void setHeight(int height);
int getWidth();
int getHeight();
int getPixelSize();
Pixel getPixel(int count);
private:
std::vector<Pixel> pixels;
int Height;
int Width;
};
#endif
and my FrameHolder class header is like so:
#pragma once
//Hold all captured frames containing data
#include "stdafx.h"
#include <gl/gl.h>
#include "PreviewWindow.h"
#include <iostream>
#include <sstream>
#include <vector>
#include "FrameHolder.h"
#include "custFrame.h"
#include "Pixel.h"
#ifndef FRAMEHOLDER_H
#define FRAMEHOLDER_H
class FrameHolder {
public:
FrameHolder();
static FrameHolder* instance();
void addFrame(IDeckLinkVideoFrame* fram);
void calibrate(custFrame fram);
int numFrames();
void setWidth(int width);
void setHeight(int height);
static FrameHolder *inst;
bool calibrating;
int getHeight();
int getWidth();
bool isCalibrating();
private:
//Member variables
int Width;
int Height;
std::vector<IDeckLinkVideoFrame *>frames;
};
#endif
In my FrameHolder class passing a custFrame object to a function to store that frame in the object does not seem to work. I get a compiler error ("syntax error: identifier 'custFrame' line 24"). However in my custFrame class, passing a Pixel object to be stored as part of a frame works wonderfully. Am I missing something? I've seen this post but it didn't help much.
The problem is caused by the presence of
#include "FrameHolder.h"
in both the .h files. Because of that, the definition of custFrame is not seen before the definition of FrameHolder.
Passing by pointers/references is probably what you should be doing here. As for the syntax error, it might be that the custFrame class is not declared properly when you include it in the header.
I really don't understand how to fix this redefinition error.
COMPILE+ERRORS
g++ main.cpp list.cpp line.cpp
In file included from list.cpp:5:0:
line.h:2:8: error: redefinition of âstruct Lineâ
line.h:2:8: error: previous definition of âstruct Lineâ
main.cpp
#include <iostream>
using namespace std;
#include "list.h"
int main() {
int no;
// List list;
cout << "List Processor\n==============" << endl;
cout << "Enter number of items : ";
cin >> no;
// list.set(no);
// list.display();
}
list.h
#include "line.h"
#define MAX_LINES 10
using namespace std;
struct List{
private:
struct Line line[MAX_LINES];
public:
void set(int no);
void display() const;
};
line.h
#define MAX_CHARS 10
struct Line {
private:
int num;
char numOfItem[MAX_CHARS + 1]; // the one is null byte
public:
bool set(int n, const char* str);
void display() const;
};
list.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "list.h"
#include "line.h"
void List::set(int no) {}
void List::display() const {}
line.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "line.h"
bool Line::set(int n, const char* str) {}
void Line::display() const {}
You need to put include guards in your headers.
#ifndef LIST_H_
#define LIST_H_
// List.h code
#endif
In list.cpp, you are including both "line.h" and "list.h". But "list.h" already includes "line.h" so "list.h" is actually included twice in your code. (the preprocessor is not smart enough to not include something it already has).
There are two solutions:
Do not include "list.h" directly in your list.cpp file, but it is a practice that does not scale: you have to remember what every of your header file includes and that will be too much very quickly.
use include guards, as explained by #juanchopanza
You are including "line.h" twice, and you don't have include guards in your header files.
If you add something like:
#ifndef LINE_H
#define LINE_H
... rest of header file goes here ...
#endif
to your header files, it will all work out fine.
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.