help with x and y getposition class

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.
The best way to represent x and y values would be through the use of a pair for each object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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
     }
}


Documentation for pairs can be found over here: http://www.cplusplus.com/reference/utility/pair/
thanks, however i now am getting: error C2011: 'creature' : 'class' type redefinition
creature.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <stdexcept>
#include <windows.h>
#ifndef CREATURE_H
#define CREATURE_H
using namespace std;

class creature
{
private:
	pair<int, int> position;
public:
	creature();
	int getX();
	int getY();
};

#endif 

creature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "creature.h"
#include <utility>     //Pairs
using namespace 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
	}
};

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include"creature.h"

using namespace std;

int main()
{
	creature creat;

	cout << creat.getX() << creat.getY() << endl;


	return 0;
}
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.
Topic archived. No new replies allowed.