Code Issue

I am getting a few errors with my code, I know that I am calling for files multiple times after searching around I am just un-sure how I would fix my code.

"Error C2011: 'Actor' : 'class' type redefinition.

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
26
27
28
29
30
31
32
33
34
//THIS IS MY source.cpp
#include "libtcod.hpp"
#include "Actor.h"
#include "Map.h"
#include "Engine.h"

Engine engine;

int main() 
{
	int playerx = 40, playery = 25;
	TCODConsole::initRoot(80, 50, "libtcod C++ tutorial", false);
	while (!TCODConsole::isWindowClosed()) 
	{
		TCOD_key_t key;
		TCODSystem::checkForEvent(TCOD_EVENT_KEY_PRESS, &key, NULL);
		switch (key.vk) 
		{
			case TCODK_UP: playery--; 
				break;
			case TCODK_DOWN: playery++; 
				break;
			case TCODK_LEFT: playerx--; 
				break;
			case TCODK_RIGHT: playerx++; 
				break;
		default:break;
		}
		TCODConsole::root->clear();
		TCODConsole::root->putChar(playerx, playery, '@');
		TCODConsole::flush();
	}
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
//THIS IS MY Actor.h
class Actor 
{
public:
	int x, y; // position on map
	int ch; // ascii code
	TCODColor col; // color

	Actor(int x, int y, int ch, const TCODColor &col);
	void render() const;
};

1
2
3
4
5
6
7
8
9
10
11
12
//THIS IS MY Actor.cpp
#include "libtcod.hpp"
#include "Actor.h"

Actor::Actor(int x, int y, int ch, const TCODColor &col) : x(x), y(y), ch(ch), col(col)
{}

void Actor::render() const
{
	TCODConsole::root->setChar(x, y, ch);
	TCODConsole::root->setCharForeground(x, y, col);
}

Theres a few others but all are the same error I am hoping I can use the solution to fix the others.
Put the following syntax into your "Actor.h"

1
2
3
4
5
6
#ifndef // this is where you put the name, typically in caps. I prefer to add _LOCK (aka ACTOR_LOCK)
#define // same name

// class code goes here

#endif 


What this little bit of syntax does is it helps your files recognize that they already have "Actor.h" defined within them maybe via another file that was using it, or another file that had it included that they included, and won't try to include it again.
Last edited on
It would be easier to find the error if you have also posted a few lines of build log above the error you are mentioning.

Anyway, it seems that you have actor.h included (directly or indirectly) in one of the other files that you are including ("libtcod.hpp", "Map.h", "Engine.h")
Last edited on
Ok that fixed the 2 issues i had with that, i apologize for seeming newbie but cant run from the fact that i AM........................ lol

I am still get 2 others errors i didnt pay any attention to until now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//MAP.h
struct Tile
{
	bool canWalk; // Can we walk through this tile?
	Tile() : canWalk(true) {}
};
#ifndef MAP_LOCK
#define MAP_LOCK
class Map
{
public :
	int width, height;

	Map(int width, int height);
	~Map();
	bool isWall(int x, int y) const;
	void render() const;
protected :
	Tile *tiles;

	void setWall(int x, int y);
};
#endif 


With this line im getting pretty much the same error but instead its for a struct --'Tile :'struct' type redefinition.
The "Tile" is called in my Map.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
26
27
28
29
30
31
32
33
34
35
36
37
38
//MAP.CPP
#include "libtcod.hpp"
#include "Map.h"

Map::Map(int width, int height) : width(width), height(height) 
{
	tiles = new Tile[width*height];
	setWall(30, 22);
	setWall(50, 22);
}

Map::~Map()
{
	delete[] tiles;
}

bool Map::isWall(int x, int y) const
{
	return !tiles[x + y*width].canWalk;
}

void Map::setWall(int x, int y)
{
	tiles[x + y*width].canWalk = false;
}

void Map::render() const
{
	static const TCODColor darkWall(0, 0, 100);
	static const TCODColor darkGround(50, 50, 150);

for (int x = 0; x < width; x++)
{
	for (int y = 0; y < height; y++)
	{
		TCODConsole::root->setCharBackground(x, y, isWall(x, y) ? darkWall : darkGround);
	}
}


Nono the preprocessor if, then. else is supposed to go before EVERYTHING in the header file.

1
2
3
4
5
6
#ifndef
#define

// class AND struct code

#endif 


Try that and see if it gets rid of your struct redefinition error.
Last edited on
Topic archived. No new replies allowed.