I'm having a problem with #including files in C++. When I try to compile the following code
#ifndef TILE_H_INCLUDED
#define TILE_H_INCLUDED
#include "location.h"
#include "Thing.h"
#include "Container.h"
#include <string>
using namespace std;
class Tile //VIRTUAL CLASS
{
protected:
Container onTile; //holds objects that are on the tile
Location * loc; //location
Tile* N; //links to Tiles of that direction (null if nothing)
Tile* E;
Tile* S;
Tile* W;
public:
//no constructor because superclass?
//loc can't move
//actions
bool placeOnTile(Thing * i){return onTile.addItem(i);}
//put a thing on the tile (on the floor)
Thing* takeFrmTile(int i){return onTile.movItem(i);}
//take a thing from the tile (from the floor)
Thing* access(int i) {return onTile.getItem(i);}
//gets an item, but doesn't move it (for like, flipping switches)
//direction setters/getters
void setLoc(Location* i){loc = i;}
void setN(Tile* i){N = i;}
void setE(Tile* i){E = i;}
void setS(Tile* i){S = i;}
void setW(Tile* i){W = i;}
Location* getLoc(){return loc;}
Tile* getN(){return N;}
Tile* getE(){return E;}
Tile* getS(){return S;}
Tile* getW(){return W;}
//displays
void dispOnTile(){onTile.allDisplay();}
void dispSingle(int i){onTile.singleDisplay(i);}
};
I get the error message that "Container" and "Thing" are not defined. Why is this? The #includes look to me like they are coded correctly, and they've worked in the past. I assumed it might be an issue of the #included files being not end-bracketed correctly or not using the same namespace, but they are ended correctly (with a }; ) and they are using the standard namespace. What's up? I can't find an answer and I know it's got to be something simple. For the record, the #included files are below:
#ifndef CONTAINER_H_INCLUDED
#define CONTAINER_H_INCLUDED
#include "Thing.h"
using namespace std;
class Container
{
private:
Thing ** contents; //array of pointers to Things
int numItems; //count item
int maxSize; //maxSize
public:
//constructor
Container(int i) {contents = new Thing*[i]; numItems = 0; maxSize=i;}
//sets num of items (for set-size bags)
Container() {contents = new Thing*[100]; numItems = 0; maxSize=100;}
//creates array of things
~Container() {delete contents;} //cleanup
//actions
bool addItem(Thing* th); //adds item to bag (really just rereferences the pointer)
bool rmvItem(int i); //removes item in array pos i
Thing* getItem(int i); //returns a pointer to item at array pos i
Thing* movItem(int i); //moves an item (deletes it and returns it)
//construction tools
void setMax(int i){delete contents; contents = new Thing*[i];}
//displays
void allDisplay(); //displays entire contents of container, numerated
void singleDisplay(int i); //displays content item i
};
#endif // CONTAINER_H_INCLUDED
#ifndef LOCATION_H_INCLUDED
#define LOCATION_H_INCLUDED
#include <string>
#include <sstream>
#include "Tile.h"
using namespace std;
class Location //stores xy coordinates of something
{
int x; //0 is NOT on map
int y;
Tile* ti; //Locations contain pointers to tiles
public:
//constructors (mainly for debug)
Location(){x=y=0;} //put object OUT OF MAP
Location(int ix, int iy){x=ix;y=iy;} //put object AT loc on map
//setters
void setX(int ix){x=ix;} //sets x
void setY(int iy){y=iy;} //sets y
void setT(Tile*i){ti=i;} //sets Tile
//getters
int getX() {return x;}
int getY() {return y;}
string getloc() //return location as a string, separated by a comma
{
ostringstream locxy; //create stringstream obj to handle input
locxy << getX() << "," << getY() << ". "; //put x, space, y into stringstream
string locret = locxy.str(); //convert stringstream to string
return locret; //return string
}
};
#endif // LOCATION_H_INCLUDED
#ifndef THING_H_INCLUDED
#define THING_H_INCLUDED
#include <string>
#include "location.h"
using namespace std;
class Thing //superclass that will be the base for objects
{
protected:
Location * loc; //location (in or out of map)
string name; //name
string desc; //description
bool deletable; //deletable (for undestructible items)
bool takeable; //if you can put it in your inv
bool hasInv; //returns true if the item has an inventory
public:
//constructor/destructor (debug only)
Thing() //sets initial values
{loc = new Location(0, 0);
name = "Uninitialized";
deletable = takeable = true;
}
Thing(int ix, int iy) //sets location
{loc = new Location(ix, iy);
name = "Uninitialized";
deletable = takeable = false;}
~Thing() {delete loc;} //deletes allocated data
//getters
Location* getLoc() {return loc;} //returns the location
string getDesc(){return desc;} //returns the description
bool getDel(){return deletable;} //returns deletable status
bool getTake(){return takeable;} //returns takeable status
string getName(){return name;} //returns name
string dispLoc(){return loc->getloc();} //displays location
//setters
void setName(string s){name = s;} //sets name
void setDel(bool b){deletable = b;} //sets deletability
void setDesc(string d) {desc = d;} //sets desc
void setLoc(Location* l) {loc = l;} //sets loc
void setTake(bool b){takeable = b;} //sets takeability
//accessors
};
#endif // THING_H_INCLUDED
I believe this is because you have recursive dependincies. That is, your classes all depend on each other, which means that at some point one of the classes will not be able to compile, because in order to get compiled it will need declaration of a class, but, it will not be able to find the declaration, because it is in the header file that is already up there in the stack of "#include" and thus because of guarding "#ifdef" becomes empty.
To give you the example.
To compile Tile, you need declaration of Location, so, naturally, you #include "location.h". But, to compile declaration of Location, you need declaration of Tile, so, you #include "Tile.h". But Tile.h has been #include-ed already, so there is no declaration for it!
The way to fight such circular dependencies is to use incomplete class declaration. For example, instead of including location.h into Tile.h, write
class Location;
class Tile
{
Location* loc;
}
This works as long as Location is only used to declare a pointer or reference and no members of Location class are accessed.
Then, in you 'Tile.c' file you can #include "location.h" and allow your inplementation of Tile methods access to Location members.
You have an include-loop
Tile.h includes
location.h
Thing.h
Container.h
Container.h includes Thing.h
Thing.h includes location.h
location.h includes Tile.h
Oops.
So let us say in the .cpp, the first to be included is Container.h. Then that includes Thing.h before it declares anything. Thing.h in turn includes location.h before it declares anything. Then that includes Tile.h before it declares anything. In Tile.h, all the include guards make the recursive includes no-ops.
And thus in Tile.h, Thing and Container are unknown.
Related
I am making a school assignment, but I am getting a strange error. I have tried to google it, but nothing helped.
So I have a file called main.cpp. Within this file I have some includes and code.
This:
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
#include "RentalAdministration.h"
#include "Limousine.h"
#include "Sedan.h"
void addTestDataToAdministration(RentalAdministration* administration)
{
string licencePlates[] = {"SD-001", "SD-002", "SD-003", "SD-004", "LM-001", "LM-002"};
for (int i = 0; i < 4; i++)
{
Car* sedan = new Sedan("BMW", "535d", 2012 + i, licencePlates[i], false);
administration->Add(sedan);
}
for (int i = 4; i < 6; i++)
{
Car* limousine = new Limousine("Rolls Roys", "Phantom Extended Wheelbase", 2015, licencePlates[i], true);
administration->Add(limousine);
}
}
int main( void )
{
RentalAdministration administration;
addTestDataToAdministration(&administration);
}
So the compiler tells me that the variable: "RentalAdministration administration" does not exist.
So if we have look in my rentaladministration header. We see this:
#ifndef RENTALADMINISTRATION_H
#define RENTALADMINISTRATION_H
#include <vector>
#include "car.h"
class RentalAdministration
{
private:
std::vector<Car*> Cars;
Car* FindCar(std::string licencePlate);
Car* FindCarWithException(std::string licencePlate);
public:
std::vector<Car*> GetCars() const {return Cars;}
bool Add(Car* car);
bool RentCar(std::string licencePlate);
double ReturnCar(std::string licencePlate, int kilometers);
void CleanCar(std::string licencePlate);
RentalAdministration();
~RentalAdministration();
};
#endif
This is the exact error:
src/main.cpp:18:34: error: variable or field ‘addTestDataToAdministration’ declared void
void addTestDataToAdministration(RentalAdministration* administration)
^
src/main.cpp:18:34: error: ‘RentalAdministration’ was not declared in this scope
src/main.cpp:18:56: error: ‘administration’ was not declared in this scope
void addTestDataToAdministration(RentalAdministration* administration)
Help will be appreciated!
Edit:
I am getting warnings in sublime for the Sedan and Limousine headers. Something that has to do with some static constants. I think it was called a GNU extension. Maybe it has something to do with it.
Even when I comment the call of that function out. I get the same error.
I am calling that function nowhere else.
Some people say that the cause might be in these headers:
#ifndef LIMOUSINE_H
#define LIMOUSINE_H
#include "Car.h"
//c
class Limousine : public Car
{
private:
bool needsCleaning;
bool hasMiniBar;
static const double priceperkm = 2.5;
public:
double Return(int kilometers);
void Clean();
bool GetHasMiniBar() const { return hasMiniBar;}
void SetHasMiniBar(bool value) {hasMiniBar = value;}
Limousine(std::string manufacturer, std::string model, int buildYear, std::string licencePlate, bool hasminiBar);
~Limousine();
};
#endif
2:
#ifndef SEDAN_H
#define SEDAN_H
#include "Car.h"
//c
class Sedan : public Car
{
private:
int lastCleanedAtKm;
bool hasTowBar;
bool needsCleaning;
static const double priceperKm = 0.29;
public:
void Clean();
int GetLastCleanedAtKm() const {return lastCleanedAtKm;}
void SetLastCleanedAtKm(bool value){ lastCleanedAtKm = value;}
bool GetHasTowBar() const {return hasTowBar;}
void SetHasTowBar(bool value) {hasTowBar = value;}
bool GetNeedsCleaning() const {return needsCleaning;}
void SetNeedsCleaning(bool value){needsCleaning = value;}
Sedan(std::string manufacturer, std::string model, int buildYear, std::string licencePlate, bool hastowBar);
~Sedan();
};
#endif
class Limousine : public Car
{
private:
static const double priceperkm = 2.5;
...
}
Remove the static and declare the member simply as const double, example:
class Limousine : public Car
{
private:
const double priceperkm = 2.5;
...
}
The error message ‘RentalAdministration’ was not declared in this scope indicates that the right header file for RentalAdministration was not included. Check the file names to make sure class declaration for RentalAdministration is in the right file.
Restarting the terminal has somehow solved this error. I got another error this time, which I solved already. I missed the destructor. It stood in the header file, but not in the cpp file.
Buggy terminals...
I have a weightedDirectedGraph class and a vertex class in their own header file, weightedDirectedGraph.h. This is it:
#ifndef GRAPH
#define GRAPH
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include "minHeapVertex.h"
using namespace std;
class vertex
{
public:
string data;
list<vertex *> neighbors;
bool known;
int distance, id;
vertex * path;
vertex(string x)
{
data = x;
}
};
class weightedDirectedGraph
{
private:
list<vertex *> vertexList;
vector<vector<int> > edgeWeights; //2D vector to store edge weights
int idCount;
weightedDirectedGraph()
{
idCount = 0;
}
vertex * findVertex(string s);
void dijkstrasAlg(vertex * s);
public:
void addVertex(string x);
//adds bi-directional edges
void addWeightedEdge(string x, string y, int weight);
};
#endif
And I have a minHeapVertex class in a minHeapVertex.h file that will be used as a priority queue in Dijkstra's algorithm. This is the file:
#ifndef MIN_HEAP_VERTEX
#define MIN_HEAP_VERTEX
#include <iostream>
#include <vector>
#include "weightedDirectedGraph.h"
using namespace std;
class minHeapVertex
{
public:
explicit minHeapVertex(int capacity = 100)
:heapArray(capacity + 1), currentSize{ 0 } {}
bool isEmpty() const
{
return (currentSize == 0);
}
vertex * getMinVertex() const; //getting C2143 error here that says I'm missing a semi-colon before '*'. Doesn't make sense though.
void insert(vertex * insertItem);
void deleteMin();
vertex * deleteAndReturnMin();
void makeEmpty()
{
currentSize = 0;
}
void decreaseKey(int index, int decreaseValue);
void remove(int index);
private:
void buildHeap();
void percolateDown(int hole);
vector<vertex *> heapArray;
int currentSize;
};
#endif
I"m getting a lot of compiling errors (with the first one being a C2143 error on the getMinVertex() declaration) and I think it may have something do with trying to access the vertex class in minHeapVertex.h. Can someone show me what I'm doing wrong? Been at it for hours, tried forward declaring the vertex class, tried removing some of the includes "", looked up the error codes and changed things, but nothing is working and just end up with a bunch of errors.
Problem:
OP has a circular dependency between minHeapVertex.h and weightedDirectedGraph.h.
Solution:
Eliminate the dependency.
minHeapVertex.h defines minHeapVertex. minHeapVertex requires vertex.
weightedDirectedGraph.h defines vertex and weightedDirectedGraph. Neither require minHeapVertex.
Three possibilities at this point:
Spin vertex off into its own vertex.h header. minHeapVertex.h and weightedDirectedGraph.h both include vertex.h and not each other.
weightedDirectedGraph.h does not require minHeapVertex.h, so remove #include "minHeapVertex.h" from weightedDirectedGraph.h to break the circle.
forward definition of class vertex; in minHeapVertex.h and the removal of #include "weightedDirectedGraph.h" from minHeapVertex.h.
Solution 1 is preferred. Giving vertex its own header may prevent future problems. 2 is easiest to implement. 3 is pretty stupid and not recommended.
Why circular dependency prevented minHeapVertex from seeing vertex:
To make this easier to see, I've removed all of the other includes from the header files.
Here's my idiotic little test.cpp
#include "weightedDirectedGraph.h"
int main(int argc, char * argsv[])
{
return 0;
}
The compiler will make a little temp file of test.cpp. It will then start parsing until it finds an include directive. The included file is copy-pasted into the temp file at the include statement. So the temp file looks sort of like this:
#define GRAPH
#include "minHeapVertex.h"
using namespace std;
class vertex
{
public:
string data;
list<vertex *> neighbors;
bool known;
int distance, id;
vertex * path;
vertex(string x)
{
data = x;
}
};
class weightedDirectedGraph
{
private:
list<vertex *> vertexList;
vector<vector<int> > edgeWeights; //2D vector to store edge weights
int idCount;
weightedDirectedGraph()
{
idCount = 0;
}
vertex * findVertex(string s);
void dijkstrasAlg(vertex * s);
public:
void addVertex(string x);
//adds bi-directional edges
void addWeightedEdge(string x, string y, int weight);
};
int main(int argc, char * argsv[])
{
return 0;
}
The compiler parses down a little further and sees the include of minHeapVertex.h and copy-pastes so you get this:
#define GRAPH
#define MIN_HEAP_VERTEX
#include "weightedDirectedGraph.h"
using namespace std;
class minHeapVertex
{
public:
explicit minHeapVertex(int capacity = 100)
:heapArray(capacity + 1), currentSize{ 0 } {}
bool isEmpty() const
{
return (currentSize == 0);
}
vertex * getMinVertex() const; //getting C2143 error here that says I'm missing a semi-colon before '*'. Doesn't make sense though.
void insert(vertex * insertItem);
void deleteMin();
vertex * deleteAndReturnMin();
void makeEmpty()
{
currentSize = 0;
}
void decreaseKey(int index, int decreaseValue);
void remove(int index);
private:
void buildHeap();
void percolateDown(int hole);
vector<vertex *> heapArray;
int currentSize;
};
using namespace std;
class vertex
{
public:
string data;
list<vertex *> neighbors;
bool known;
int distance, id;
vertex * path;
vertex(string x)
{
data = x;
}
};
class weightedDirectedGraph
{
private:
list<vertex *> vertexList;
vector<vector<int> > edgeWeights; //2D vector to store edge weights
int idCount;
weightedDirectedGraph()
{
idCount = 0;
}
vertex * findVertex(string s);
void dijkstrasAlg(vertex * s);
public:
void addVertex(string x);
//adds bi-directional edges
void addWeightedEdge(string x, string y, int weight);
};
int main(int argc, char * argsv[])
{
return 0;
}
That gets parsed down to #include "weightedDirectedGraph.h", but fortunately GRAPH has been defined, so most of weightedDirectedGraph.h gets left out. If it hadn't, Everything in weightedDirectedGraph.h would have been defined again and minHeapVertex.h would once again been included over and over and eventually the compiler would crash or tell you to expletive deleted off with a politely worded error message.
Anyway, we can already see what's gone wrong in the above code trace: minHeapVertex needs to know type vertex, but that won't be defined for another 20 lines or so.
If test.cpp had been written as
#include "minHeapVertex.h"
int main(int argc, char * argsv[])
{
return 0;
}
The header files would have been included in the other order and it would have compiled, giving a false sense of security until one day you wrote a program that included weightedDirectedGraph.h first. In other words, the library works until it doesn't, and you didn't change a line of the library's code. Have fun pulling your hair out.
Avoid circular dependencies, circular references and circular saws. All three can rip you up pretty bad.
On to using namespace std; This evil little shortcut takes EVERYTHING in the std namespace and adds it to the global namespace. If you had a function named reverse, now you have to deal with potential overload conflicts with std::reverse. The standard library is huge. There are a huge number of function, class, and variable names that are just itching to overload, override and just plain trample your stuff.
But that's your problem.
Putting using namespace std; in a header make it everyone's problem. Anyone who uses your graphing library has to wade through a minefield, and unless they take a close look at your header file and see that declaration they won't have the slightest clue.
Longer discussion can be found here. Either explicitly namespace everything (std::vector, std::string, ...) or pull in only the pieces you need and know will not conflict with your code with using. Eg:
using std::vector;
using std::string;
Do not put this in your header or someone may wind up wonder why their homebrew vector is freaking out. Probably shouldn't be homebrewing vectors, but you can't save everybody.
First post so take it easy on me :). I don't think I really need to put up any actual code for this, but let me know if I'm wrong. This is for a homework assignment in my college programming class. I am confused as to how to properly use my #include statements. Here is my file structure:
Header Files-->
header.h (Main header file, contains #include for various libraries, declares namespace, and provides my name and class info)
room.h (Blueprint for the room class)
ship.h (Blueprint for the ship class)
Source Files-->
main.cpp (Main Program)
functions.cpp (Functions for the main program)
room.cpp (Functions in the Room class)
ship.cpp (Functions in the Ship class)
Basically, my first instinct was to " #include "header.h" " in room.h, ship.h, main.cpp, and functions.cpp. Then " #include "ship.h" in ship.cpp, and " #include room.h " in room.cpp. However I began getting errors up the wazoo. I was having a similar problem during class but I had my teacher there to sort it out and I'm not exactly sure how we did it, and I also know that tons of errors usually indicates an include error.
Its annoying because I had it working somehow before I added the functions.cpp, but I really want to keep main.cpp pretty clean, so I would rather have functions in a separate file.
What is the best pattern for includes in a situation like this?
EDIT: I'll post my 3 header files
header.h
/*
Author: *********
Class : **********
Assignment : Programming Assignment 2
Description :
This program will construct a ship for the user. It accepts input from a file
containing information on various rooms. It will then check the rooms
validity and add it to the ship if it's valid. Once all of the rooms have been added,
the program will determine if the entire ship is valid and let the user know.
Certification of Authenticity :
I certify that this is entirely my own work, except where I have given
fully - documented references to the work of others.I understand the
definition and consequences of plagiarism and acknowledge that the assessor
of this assignment may, for the purpose of assessing this assignment :
-Reproduce this assignment and provide a copy to another member of
academic staff; and / or
- Communicate a copy of this assignment to a plagiarism checking
service(which may then retain a copy of this assignment on its
database for the purpose of future plagiarism checking)
*/
#ifndef header_h
#define header_h
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
#endif
room.h
#ifndef room_h
#define room_h
#include "header.h"
enum RoomType
{
UNKNOWN = -1,
BAY,
LATRINE,
CABIN,
BRIDGE,
NUM_ROOM_TYPES
};
const string ROOM_STRINGS[NUM_ROOM_TYPES] = { "Bay",
"Latrine",
"Cabin",
"Bridge"
};
class Room
{
public:
//default constructor
Room();
//constructor
Room( RoomType type, int width, int breadth, int height );
//destructor
~Room(){};
//accessors
inline RoomType getType() const { return mType; };
inline int getHeight() const { return mHeight; };
inline int getWidth() const { return mWidth; };
inline int getBreadth() const { return mBreadth; };
inline int getVolume() const { return getWidth() * getBreadth() * getHeight(); }; //currently unused
inline string getRoomName(){ return ROOM_STRINGS[mType]; };
string getDescription();
//mutators
void setType(RoomType type) {mType = type; };
void setHeight(int height) {mHeight = height; };
void setWidth(int width) {mWidth = width; };
void setBreadth(int breadth) {mBreadth = breadth; };
private:
//type of room
RoomType mType;
//floor dimensions - in feet
int mWidth;
int mBreadth;
//ceiling height - in feet
int mHeight;
};
#endif
ship.h
#ifndef ship_h
#define ship_h
#include "header.h"
const int MAX_BAY = 4;
const int MAX_LATRINE = 15;
const int MAX_BRIDGE = 1;
const int MAX_CABIN = 25;
const int MIN_BAY = 1;
const int MIN_LATRINE = 1;
const int MIN_BRIDGE = 1;
const int MIN_CABIN = 0;
const int MIN_ROOM_HEIGHT = 7;
const int MIN_ROOM_AREA = 20;
class Ship{
public:
Ship();
bool addRoom(const Room& theRoom);
string getDescription();
//Accessors
int getNumBays(){ return bayTotal; };
int getNumLatrines(){ return latrineTotal; };
int getNumBridges(){ return bridgeTotal; };
int getNumCabins(){ return cabinTotal; };
int getTotalSquareFootage(){ return totalSquareFootage; };
private:
Room Bay[MAX_BAY];
Room Latrine[MAX_LATRINE];
Room Bridge[MAX_BRIDGE];
Room Cabin[MAX_CABIN];
int bayTotal;
int latrineTotal;
int bridgeTotal;
int cabinTotal;
int totalSquareFootage;
bool isShipValid();
void addSquareFootage(float);
};
#endif
What kind of errors? Your issue might be including the same header more than once.
Try adding this to each header:
#ifndef ROOM_H
#define ROOM_H
... code ...
#endif
To be clear the 'ROOM_H' above needs to be unique to each header.
If you use #include "room.h" in different cpp files then you probably get a linker error because this below here is not a type declaration.
const string ROOM_STRINGS[NUM_ROOM_TYPES] = { "Bay",
"Latrine",
"Cabin",
"Bridge"
};
You are creating and allocating a variable with name ROOM_STRINGS. By declaring it in different cpp files you will have multiple copies of the same global variable which is an error. You could replace it with
static const string ROOM_STRINGS[NUM_ROOM_TYPES] = { "Bay",
"Latrine",
"Cabin",
"Bridge"
};
You will still have multiple copies but each cpp file will have its own private copy. A better solution is to move this declaration into the room cpp file together with the code of the getRoomName.
Or you could declare ROOM_STRINGS as extern in the header and then you still need to add the variable allocation in a cpp file.
I've read tons of similar questions,but I still can not figure it out. Error I get in QT creator when I try to compile this is next: "Grid has not been declared",although I included the Grid class header in the bunny class which displays this error. Here are my codes:
(Error is produced in function line "void becomeVampire(Bunny *& , int &,Grid &);" ,which is fifth if you count from down to up, in first(BUNNY) class, and also in function "void convertNeighbourToVampire(Bunny * const, int &, Grid &);" which is first from down to up: Both of these functions use reference to Grid class as one of parameters)
#ifndef BUNNY_H
#define BUNNY_H
#include <conio.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <windows.h>
#include "Position.h"
#include "Grid.h"
int GetRandom(int x);
class Bunny:public Position
{
private:
static const std::vector <std::string> F_names; //all the female names
static const std::vector <std::string> M_names; //all the male names
static const std::vector <std::string> colors; //all the colors
bool isMale= GetRandom(2)<1 ? true : false; //at creation,bunnies gender is randomly chose//50%chance
bool isVampire= GetRandom(100)< 2 ? true : false; //2% chance for birth of vamp bunny
std::string name;
std::string color;
std::string sex;
std::string vampire;
int age;
void Inner(int & TVamps); //use this inside of constructors,assign all needed info to a bunny
void half(Bunny *&,int &); //halfs the population randomly
void increaseAge(); //called by ageUp() function,increases the age of a particular bunny object
public:
Bunny * Next; //pointer to next bunny in linked list
Bunny(int & TVamps); //DEFAULT CONSTRUCTOR
Bunny(std::string C,int & TVamps); //CHILDREN CONSTRUCTOR
~Bunny(); //DESTRUCTOR
//NEXT is self explanatory
bool isMatureMale() const {if( (this->age>2) &&(isMale) &&(!isVampire) ){return true;} return false;}
bool isMatureFemale() const {if( (age>2) &&( !isMale) &&(!isVampire) ){return true;} return false;}
bool getIsMale() const {return isMale;}
std::string getName() const {return name;}
std::string getColor() const {return color;}
std::string getSex()const {return sex;}
std::string getVampire() const{return vampire;}
bool getIsVampire() const {return isVampire;}
int getAge() const {return age;}
void makeVampire(int &); //make certain bunny a vampire
bool isEmpty(Bunny *) const; //is bunny colony empty
void ageUp(Bunny *); //increase the populations age by 1
void insertBunny(Bunny *,Bunny *&, int &,int &); //add a child,parameters are: head pointer,reference of last pointer,and counter of bunnies and vampires
void insertFirstBunny(Bunny *&, int &,int &); //insert a bunny with no parrent
void printBunnies(Bunny * , int , int,int) const; //prints out the whole colony info
void removeBunny(Bunny *& ,int &, int &); //removes old bunnies
**void becomeVampire(Bunny *& , int &,Grid &); //converts bunnies to vampires**
void FoodShortage(Bunny *& ,int &); //kills half of bunnies randomly,takes pointer reference of head pointer as parameter
void massRabitKull(Bunny *&,int &); //when "k" or "K" is pressed,calls the half() function
bool isArrayOutOfBounds(int x, Bunny const * const temp); // north-1 south-2 east-3 west-4
void convertNeighbourToVampire(Bunny * const, int &, Grid &);
};
#endif // BUNNY_H
And here is the Grid class:
#ifndef GRID_H
#define GRID_H
#include "Position.h"
#include "Bunny.h"
class Grid
{
private:
char GridField[80][80];
void renewGrid(); //sets all grid positions to '.',called by constructor and updateTheGrid()
public:
Grid();
void updateTheGrid(Bunny * const); //sets all grid positions to new positions based on new bunny objects linked list
void printTheGrid() const; //prints the grid to the new file
char getGridField(int,int); //returns the character on certain position
};
#endif // GRID_H
and the last class, Position:
#define POSITION_H
#include "Grid.h"
class Position
{
private:
int row,column;
char sign;
public:
Position();
Position ( int parrentRow,int parrentColumn );
int getRow() const ;
int getColumn() const;
char getSign() const;
};
#endif // POSITION_H
I have a little problem, i probably included the class files wrongly, since i can't acces members of the enemy class. What am i doing wrong?
my cpp for class
#include "classes.h"
class Enemy
{
bool alive;
double posX,posY;
int enemyNum;
int animframe;
public:
Enemy(int col,int row)
{
animframe = rand() % 2;
posX = col*50;
posY = row*50;
}
Enemy()
{
}
void destroy()
{
alive = 0;
}
void setposX(double x)
{x = posX;}
void setposY(double y)
{y = posY;}
};
my header for class:
class Enemy;
my main:
#include "classes.h"
Enemy alien;
int main()
{
alien. // this is where intelisense tells me there are no members
}
Your main file will only see what you wrote in the header, which is that Enemy is a class. Normally, you'd declare your whole class with fields and method signatures in the header files, and provide implementations in the .cpp file.
classes.h:
#ifndef _CLASSES_H_
#define _CLASSES_H_
class Enemy
{
bool alive;
double posX,posY;
int enemyNum;
int animframe;
public:
Enemy(int col,int row);
Enemy();
void destroy();
void setposX(double x);
void setposY(double y);
};
#endif
classes.cpp:
#include "classes.h"
//....
void Enemy::destroy(){
//....
}
//....
In addition to Vlad's answer, your file with main doesn't know anything about the Enemy class, other than that it exists.
In general, the class declarations goes in the header file, and the function definitions go in another.
Consider splitting the files like:
classes.h:
#ifndef CLASSES_H
#define CLASSES_H
class Enemy
{
private:
bool alive;
double posX,posY;
int enemyNum;
int animframe;
public:
Enemy(int col,int row);
Enemy();
void destroy();
void setposX(double x);
void setposY(double y);
};
#endif//CLASSES_H
Note the "include guards" which prevent the same file from being included more than once. Good practice to use on header files, or else you get annoying compilation errors.
classes.cpp:
#include "classes.h"
Enemy::Enemy(int col,int row)
{
animframe = rand() % 2;
posX = col*50;
posY = row*50;
}
Enemy::Enemy()
{
}
void Enemy::destroy()
{
alive = 0;
}
void Enemy::setposX(double x) {x = posX;}
void Enemy::setposY(double y) {y = posY;}