Class type redefinition problem

Pages: 123
Hi, I've been programming for about half a year. I've not been programming that much though, I've been taking breaks for some weeks or something and then started again, break, start again, break, start again and so on.

Anyways, so in order to remember everything I've learnt from before since my last break was for about 2 months, I've been trying to make a textbased game since it brings up almost everything that I know in programming. But now I have a really frustrating problem that I've been trying to figure out for two days now.

Compiler error: \my textbased game\human warrior (definition).cpp(5): error C2011: 'HumanWarrior' : 'class' type redefinition

Some of the code:

my textbased game.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
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Human Warrior (declaration).h"

using namespace std;

int main()
{
	raceSelectFunction();
	classSelectFunction();
	cout << "Now that you've chosen race and class you have to choose a name for your character. What do you want the name of your character to be?\n\n";
	cin >> characterName;
	system ("CLS");
	cout << "Your name is " << characterName << ".\n\n";
	cout << "Where are you from? Type in the name of the city that you live in\n\n";
	cin >> characterCity;
	system ("CLS");
	cout << "You are from " << characterCity << ".";
	Continue();
	characterNameClassRaceCity();
	cout << "\n\nLet's continue to the actual game";
	Continue();
	

string quit;
cin >> quit;
return 0;
}


Human Warrior (declaration).h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class HumanWarrior
{
public:
	HumanWarrior(int startHealth, int startStrength, int startAgility, int startStamina);
	void stats();
	void levelUp();
	void afterLvlUp();

private:
	int health;
	int strength;
	int agility;
	int stamina;
};



Human Warrior (definition).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
#include "stdafx.h"
#include "Human Warrior (declaration).h"

class HumanWarrior
{ //(Here's the problem!)
	HumanWarrior::HumanWarrior(int startHealth, int startStrength, int startAgility, int startStamina):
	health (startHealth), strength (startStrength), agility (startAgility), stamina (startStamina)
	{
	}

	void HumanWarrior::stats()
	{
		cout << "\n\nYour stats are:\n\n" << "Health: " << health << "\n" << "Strength: " << strength << "\n" << "Agility: " << agility << "\n" << "Stamina: " << stamina << "\n\n";
	}

	void HumanWarrior::levelUp()
	{
		health ++;
		strength ++;
		agility ++;
		stamina ++;
		cout << "Level Up!\n\n";
	}

	void HumanWarrior::afterLvlUp()
	{
		cout << "Your stats are now, after level up, \n\nHealth: " << health << "\n" << "Strength: " << strength << "\n" << "Agility: " << agility << "\n" << "Stamina: " << stamina;
	}

};



I think I've given enough information. If not, please tell me.

Thanks.


Note: If there are any errors like if a function's name or something is wrong, that's only because I've translated some of the code from swedish, so don't worry about that. Only worry about the error above.
* dies at the sight of yet another console based game attempt *
When will people learn the console is a terrible medium for games >_<


Anyway...

1
2
3
// in your cpp file...
//class HumanWarrior    // get rid of this
//{ 


The 'class' bit only goes when you're defining the class body (ie: it only goes in the header).

In the cpp file where you're giving the function bodies, you don't put it there.
You defined your HumanWarrior class twice.

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
#include "stdafx.h"
#include "Human Warrior (declaration).h"

class HumanWarrior // <--- is not needed
{ //(Here's the problem!)
	HumanWarrior::HumanWarrior(int startHealth, int startStrength, int startAgility, int startStamina):
	health (startHealth), strength (startStrength), agility (startAgility), stamina (startStamina)
	{
	}

	void HumanWarrior::stats()
	{
		cout << "\n\nYour stats are:\n\n" << "Health: " << health << "\n" << "Strength: " << strength << "\n" << "Agility: " << agility << "\n" << "Stamina: " << stamina << "\n\n";
	}

	void HumanWarrior::levelUp()
	{
		health ++;
		strength ++;
		agility ++;
		stamina ++;
		cout << "Level Up!\n\n";
	}

	void HumanWarrior::afterLvlUp()
	{
		cout << "Your stats are now, after level up, \n\nHealth: " << health << "\n" << "Strength: " << strength << "\n" << "Agility: " << agility << "\n" << "Stamina: " << stamina;
	}

};


Remove the class HumanWarrior { };, and it should compile properly.


EDIT: Crap, Disch beat me.
(offtopic) Btw, thanks for introducing me to SFML Disch.
Last edited on
Haha, I'm not making the game for anything other than to remember what I've learned and setting it into practice. Making textbased games is great for that! :)

Thanks though :)

I'm not sure on what to remove though now when I've removed 'class' from

1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include "Human Warrior (deklaration).h"

HumanWarrior
{
	HumanWarrior::HumanWarrior(int startHealth, int startStrength, int startAgility, int startStamina):
	health (startHealth), strength (startStrength), agility (startAgility), stamina (startStamina)
	{
	}de]


I get rid of the compiler error but i get three new ones

human warrior (definition).cpp(4): error C2059: syntax error : ')'
human warrior (definition).cpp(5): error C2143: syntax error : missing ';' before '{'
human warrior (definition).cpp(5): error C2447: '{' : missing function header (old-style formal list?)

I'm using Visual Express 2010 by the way.
1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include "Human Warrior (declaration).h"

HumanWarrior   // Those don't need to be there either.
{                       //  
	HumanWarrior::HumanWarrior(int startHealth, int startStrength, int startAgility, int startStamina):
	health (startHealth), strength (startStrength), agility (startAgility), stamina (startStamina)
	{
	}de]



When defining class functions you don't need to put it in brackets

1
2
3
4
5
6
7
8
9
HumanWarrior::HumanWarrior(int startHealth, int startStrenght, int startAgility, int startStamina):
....

void HumanWarrior::Stats()
{
   ....
}

....


is all you need in the definition file.
Last edited on
Oh, now I get it. Thanks!

Now I have a new problem "cout : undeclared identifier" in my Human Warrior (definition).cpp
I've tried including iostream but it doesn't work...
Last edited on
you need to include
using namespace std; above your main function.

that or you can use it like this
std::cout << string << std::endl;
Yeah, I've done that, this error is in the Human Warrior (definition).cpp-file. Sorry, I didn't make that clear.
You also need to reference using namespace std; in there, otherwise it doesn't know the namespace exists. In the Human Warrior (declaration).h file you should

1
2
#include <iostream>
using namespace std;
Last edited on
Thanks! Now everything works! Thank you so much, this has been bothering me for two days!
No problem ^^
Eh, btw, Disch... What do you suggest as a project for someone at my level of knowledge in programming? Maybe something more of a program than a game? Or if anyone else have any suggestions?
Last edited on
If you want to make a game, then make a game.

But making a game in the console is harder than making a game with graphics. The console (and specifically, the C++ standard lib) isn't designed to do the kind of things that you'd expect to do in games.

You can make it work, but it's be a lot more work than it needs to be.

I say... if you want to make a game, get a library designed for making games. I often recommend SFML, as it's very easy to use.

http://www.sfml-dev.org/

getting it installed and set up in your environment will be a bit of a hurdle, but you only have to do it once, and it will be well worth it in the long run.
Ok, I'll try to learn that. I installed allegro, so I (kind of) know how to install it. But what to you do in C++ standard library? What sort of programs is it made for?
Last edited on
well vectors/lists/etc can be applied anywhere, but the cout/cin stuff is just for very basic I/O.

cout is useful for when you're making a commandline program that has little/no user interaction.

cerr is useful for dumping error messages and things for debugging purposes in any program.

And I have no idea where a practical use for cin would be. =x
Ok, well thanks :) I'm trying to install SFML right now. Is it better than allegro?
For some things. IIRC allegro is sort of like a hybrid of GUI + game lib, so allegro would probably be better if you want to have drop down menus, dialogs, and things like that.

But if you just want a 2D without widgetry, then SFML is way easier/better.
Ok, I kind of want to make a 2D RPG (like Pokémon or Final Fantasy) with dialogues and such when I've learnt the language so I'm going to go with Allegro right now. Thanks!
Last edited on
For a 2D RPG I'd recommend SFML.

By "dialogs" I meant like those "forms". Like the boxes that have push-buttons and all sorts of other widgetry. I didn't mean "dialogue" like text. You can print text with SFML just fine.

You don't really need widgetry for what you're describing, so I would go with SFML.
Last edited on
Ok, thanks again. Just seems like my compiler is too new(?!) for installing SFML, I can't find it for VS 2010 and when I tried to install the 2008 version, it would only work with 2008. Oh, well. I guess the problem will be solved just by using 2008 instead :).

Thanks :)
Last edited on
Pages: 123