Sign in to follow this  

Annoying error, i can't find out what it is?!

This topic is 4389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

c:\documents and settings\dcragg\my documents\visual studio 2005\projects\1st\1st\map.h(18) : error C2061: syntax error : identifier 'cPlayer' #include "Player.h" #include <iostream> #include <string>
using namespace std;

class Map {
public:
	void North(int) { y =+ 1; }
	void South(int) { y =- 1; }
	void East(int) { x =+ 1; }
	void West(int) { x =- 1; }
	void Show(cPlayer lame) { cout << x << ", " << y; }

private:
	int x;
	int y;
};

cPlayer is fine:
class cPlayer {
public:
	void Name(string &temp) { name = temp; } 
	string GName() { return name; }
	void ShowAll();
	int HP();
	int MP();
	void Move(cPlayer & player, Map & zap);
	string weapon(cWeapon &Name) { pWeapon = Name; }
	void Race();

private:
	string name;
	string race;
	cWeapon pWeapon; 
	int hp;
	int mp;
};

the call to it is from main
int main() {
	cPlayer * Player = new cPlayer;
	Map * zap = new Map;
	string name;
	cout << "Welcome to a DCMK game\n";
	cout << "Character Name: ";
	cin>>name;
	Player->Name(name);
	Player->Race();
	Player->ShowAll();
	Player->Move(*Player,*zap);

i done alot of tampering about with main so dont be at a lost to see why i done certain things to it lol error is the - void Show

Share this post


Link to post
Share on other sites
The player and map seem to be mutually dependent on each other. Are you forward declaring either?
Consider what the preprocessor does to a compilation unit when you try to include the other in both headers, with include guards in place.

Share this post


Link to post
Share on other sites
yea it is, in regards to whether cPlayer defined before Map. switched them and tried everything involving headers, i had similar problems before, just hit and miss - me playing with them

Share this post


Link to post
Share on other sites
if what you posted is your full code then you need to predeclare your classes by saying:

class cPlayer;
class Map;

above your classes. other than that, i dont notice anything wrong with the code.

Share this post


Link to post
Share on other sites
Quote:
Original post by dcuk
yea it is, in regards to whether cPlayer defined before Map. switched them and tried everything involving headers, i had similar problems before, just hit and miss - me playing with them


Well if you seprate it out so that you have the implementation in it's own file you could probalby get by with pass by (const) reference and forward declarations.

I.e. like:

map.hpp


class Player;//look mommy forward declared.

class Map
{
public:
void show(const Player &player)
};


map.cpp

#include "map.hpp"
#include "player.hpp"
//implement the map here, no collisions.


player.hpp

class Map;//forward declare map if needed..

class Player
{
//yadda yadda you get it :) can use map pointers and references in the interface.
};


Share this post


Link to post
Share on other sites
ok nah that didnt work, ok aint much so post it all up, hope this helps :D

Player.cpp

#include "Player.h"

void cPlayer::Move(cPlayer &Player, Map &zap) {
int input;
cout << "\n1) North\t2) South\t3) East\t4) West\t5) Cancel\n";
cin>>input;
switch(input) {
case 1:
zap.North(1);
break;
case 2:
zap.South(1);
break;
case 3:
zap.East(1);
break;
case 4:
zap.West(1);
break;
}//}
}


void cPlayer::ShowAll() {
cout << "\n\n~~~~~~~~~~~~~~~~~~~~~~~\n~~~~~~~~~~~~~~~~~~~~~~~";
cout << "\nName is: " << name;
cout << "\nRace is: " << race;
cout << "\nHP is: " << hp;
cout << "\nMP is: " << mp;
cout << "\nWeapon is: " << pWeapon.gName();
cout << "\nMax Damage is: " << pWeapon.minDam();
cout << "\nMax Damage is: " << pWeapon.maxDam();
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~\n~~~~~~~~~~~~~~~~~~~~~~~\n";
}

void cPlayer::Race() {
int num;
string temp;
cout << "\nSelect Class: \n\t\t 1) Warrior\n\t\t 2) Wizard \n\t\t 3) Dwarf\n";
cin>>num;
switch(num) {
case 1:
cout << "WARRIOR!";
temp = "Warrior";
mp = 20;
hp = 100;
pWeapon.Name("Long Sword");
pWeapon.minDam(2);
pWeapon.maxDam(6);
break;
case 2:
cout << "Wizard!";
temp = "Wizard";
mp = 60;
hp = 50;
pWeapon.Name("Staff");
pWeapon.minDam(2);
pWeapon.maxDam(6);
break;
case 3:
cout << "Dwarf!";
temp = "Dwarf";
mp = 40;
hp = 65;
pWeapon.Name("Pole");
pWeapon.minDam(2);
pWeapon.maxDam(6);
break;
}
race = temp;
}



Map.h - no .cpp

#pragma once

//#ifndef MAP_H
//#define MAP_H
//
#include "Player.h"
#include <iostream>
#include <string>

using namespace std;

class Map {
public:
void North(int) { y += 1; }
void South(int) { y -= 1; }
void East(int) { x += 1; }
void West(int) { x -= 1; }
void Show(cPlayer lame) { cout << x << ", " << y; }

private:
int x;
int y;
};


weapon.h

#pragma once

//#ifndef WEAPON_H
//#define WEAPON_H


#include <string>
#include <iostream>
using namespace std;

class cWeapon {
public:
void Name(string temp) { name = temp; }
string gName() { return name; }
void minDam(int min) { mnDam = min; }
int minDam() { return mnDam; }
int maxDam() { return mxDam; }
void maxDam(int max) { mxDam = max; }
void Value(int t_value) { value = t_value; }
int Value() { return value; }
private:
string name;
int mnDam;
int mxDam;
int value;
};

//#endif




#pragma once

//#ifndef PLAYER_H
//#define PLAYER_H

//#include "map.h"
#include <string>
#include <iostream>
#include "map.h"
#include "weapon.h"


using namespace std;
//class Map;

class cPlayer {
public:
void Name(string &temp) { name = temp; }
string GName() { return name; }
void ShowAll();
int HP();
int MP();
void Move(cPlayer & player, Map & zap);
string weapon(cWeapon &Name) { pWeapon = Name; }
void Race();

private:
string name;
string race;
cWeapon pWeapon;
int hp;
int mp;
};


//#endif





#include "Player.h"
#include "map.h"
#include <string.h>
#include <iostream>
using namespace std;

class cPlayer;
class Map;
int main() {
cPlayer * Player = new cPlayer;
Map * zap = new Map;
string name;
cout << "Welcome to a DCMK game\n";
cout << "Character Name: ";
cin>>name;
Player->Name(name);
Player->Race();
Player->ShowAll();
Player->Move(*Player,*zap);

cout << "\n";
return 0;
};




theres actually not that much so thought bung it all in, but honestly - it's probably glaringly obvious - but doing my nut in - help!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try moving your ShowAll() function to be the last of the public members in your cPlayer class. Dunno if that will do anything, but just try I guess :O

Share this post


Link to post
Share on other sites
These changes should solve your problems:
  • Since Map.h uses the cPlayer class, you should include Player.h (or forward-declare cPlayer) in Map.h, not main.cpp.
  • Since Player.h uses the class Map, it should include Map.h. Notice that this creates a circular reference. To fix that, one or both of the includes should be replaced with forward declarations.
  • Since Player.cpp uses the classes Map and cPlayer, you should include Map.h and Player.h in Player.cpp.
  • Since Player.cpp uses strings and iostreams, you should include <string> and <iostream> in Player.cpp
  • Do not include <string> in Map.h, since it doesn't use strings.
  • Do not include <iostream> in weapon.h and Player.h, since they doesn't use iostreams.
See the pattern? Files (especially header files) should be "self-sufficient". If a file uses a particular class, include its header file, otherwise don't. However, in header files, you can frequently "forward-declare" types instead of including the header file. That is usually preferable, because it helps avoid circular references and other problems.

Also,
  • Do not use "using namespace xxx;" in a header file because it will apply to any cpp file that includes it. Not a good idea.
  • Here is a technique that helps verify that header files are "self-sufficient": Always include the associated header file in a source file first (or after the PCH header file). For example, weapon.cpp (if it exists) should include weapon.h first, Player.cpp should include Player.h first, and Map.cpp (if there were one) should include Map.h first.


[Edited by - JohnBolton on December 6, 2005 2:42:47 PM]

Share this post


Link to post
Share on other sites
I recreated your project and found the error. Forward declare "class map;" in player.h above the class then include math.h beneath the class and NOT above it.

the cleaned up player.h looks like this.


#pragma once
#include <string>
#include <iostream>

#include "weapon.h"


using namespace std;
class Map;

class cPlayer {
public:
void Name(string &temp) { name = temp; }
string GName() { return name; }
void ShowAll();
int HP();
int MP();
void Move(cPlayer & player, Map & zap);
string weapon(cWeapon &Name) { pWeapon = Name; }
void Race();

private:
string name;
string race;
cWeapon pWeapon;
int hp;
int mp;
};

#include "map.h"






also you can take the forward class declerations out of main if you want as they arent neccisary. yeah, that one was a tad annoying. This is why i dont like creating classes that are dependant on each other, but i guess sometimes you cant avoid it. well gl w/ the game :)

Share this post


Link to post
Share on other sites
Quote:
Original post by EricmBrown
... then include map.h beneath the class and NOT above it.
That's not a good solution. If map.h is included at the end, why is it included at all?

Share this post


Link to post
Share on other sites

This topic is 4389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this