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

Started by
10 comments, last by JohnBolton 18 years, 4 months ago
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
Advertisement
Try changing =+ to += and =- to -=
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.
Is cPlayer declared before Map?
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
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.
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.};


HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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!
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
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]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement