Hi, I have started making a program which draws 5 characters (using the character C to represent 1 creature) on screen somewhere random within a 30x30 area. The problem I am facing is what would be the easiest way to create 5 x and y values within a class and then use creature1.getxpos() or something so that you could return the value of any of the creatures that you wanted to.
#include <utility> //Pairs
class character
{
private:
std::pair<int, int> position; /*A pair is a data type that holds two values.
In this case, it will hold two ints*/
public:
character()
{
position.first = getRandomNumber(); //Set the x position randomly
position.second = getRandomNumber(); //Set the y position randomly
}
int getX()
{
return position.first; //Return the x position
}
int getY()
{
return position.second; //Return the y position
}
}
#include "creature.h"
#include <utility> //Pairs
usingnamespace std;
class creature
{
private:
pair <int, int> position;
public:
creature()
{
position.first = 5; //Set the x position randomly
position.second = 5; //Set the y position randomly
}
int getX()
{
return position.first; //Return the x position
}
int getY()
{
return position.second; //Return the y position
}
};
The problem has to do with the fact that you re-declared the class in the .cpp file. Remember, header files are for class and member declarations. .cpp files are for definitions of those declarations made in the header file.
Also, you should include utility in the header file rather than the .cpp file, since you declare a pair in the header file.