Class help  
Author Message
Little_Dice





PostPosted: Visual C++ Language, Class help Top

I keep getting some errors due to my classes. I was hoping someone could help me with these errors.
c:\documents and settings\xx\my documents\visual studio 2005\projects\dung and drag\dung and drag\item.h(13) : warning C4996: 'strcpy' was declared deprecated
c:\program files\microsoft visual studio 8\vc\include\string.h(73) : see declaration of 'strcpy'
Message: 'This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
Compiling...
Player.cpp
c:\documents and settings\xx\my documents\visual studio 2005\projects\dung and drag\dung and drag\player.h(10) : error C2011: 'PLAYER' : 'class' type redefinition
c:\documents and settings\xx\my documents\visual studio 2005\projects\dung and drag\dung and drag\player.h(10) : see declaration of 'PLAYER'
c:\documents and settings\xx\my documents\visual studio 2005\projects\dung and drag\dung and drag\player.cpp(7) : error C2027: use of undefined type 'PLAYER'
c:\documents and settings\xx\my documents\visual studio 2005\projects\dung and drag\dung and drag\player.h(10) : see declaration of 'PLAYER'
c:\documents and settings\xx\my documents\visual studio 2005\projects\dung and drag\dung and drag\player.cpp(7) : error C2059: syntax error : ')'
c:\documents and settings\xx\my documents\visual studio 2005\projects\dung and drag\dung and drag\player.cpp(8) : error C2143: syntax error : missing ';' before '{'
c:\documents and settings\xx\my documents\visual studio 2005\projects\dung and drag\dung and drag\player.cpp(8) : error C2447: '{' : missing function header (old-style formal list )

player.h

// PLAYER VARIABLES AND FUNCTIONS

#define XL_1 0
#define XL_2 1000
#define XL_3 3000



class PLAYER: public ENTITY
{
private:

short Sex;
short Race;
char* Name;
short Class;


public:

PLAYER();

void SetSex(char newsex);
char GetSex();
void SetRace(char newrace);
char GetRace();
void SetName(char* newname);
char* GetName();
void SetCharClass(char newclass);
char GetCharClass();

};

Player.cpp
#include "stdafx.h"
#include "Player.h"




PLAYER::PLAYER()
{
SetName("Hero");
SetSex(0);

}

Createchar.cpp
#include "stdafx.h"

PLAYER player;

bool CharacterCreation()
{
int choice = 0;

cout << "What is your Characters Name \n";
char name[32];
cout << ">";
cin.getline(name,32,'\n');
player.SetName(name);

while (choice != 3)
{
cout << "\n\n\nWhat is your character's sex \n\n";
cout << "1: Male\n";
cout << "2: Female\n\n";
cout << ">";
cin >> choice;

switch (choice)
{
case 1:
{
cout << "\nYou have chosen to be male.\n";
player.SetSex(1);
break;
}
case 2:
{
cout << "\nYou have chosen to be female.\n";
player.SetSex(2);
break;
}
default: break;
}
}

//set race and rest choice to 0
choice = 0;
while (choice != 4)
{
cout << "What is your characters race. \n Elf, Dwarf, or Human. \n";
cout << "1: Elf\n";
cout << "2: Dwarf\n";
cout << "3: Human\n";
cout << ">";
cin >> choice;

switch (choice)
{
case 1:
{
cout << "\nYou have chosen to be Elf.\n";
player.SetRace(1);
break;
}
case 2:
{
cout << "\nYou have chosen to be Dwarf.\n";
player.SetRace(2);
break;
}
case 3:
{
cout << "\nYou have chosen to be Human.\n";
player.SetRace(3);
break;
}
default: break;
}
}

//set class and reset choice
choice = 0;
while (choice != 5)
{
cout << "What is your characters class. \n Cleric, Fighter, Rogue, or Wizard. \n";
cout << "1: Cleric\n";
cout << "2: Fighter\n";
cout << "3: Rogue\n";
cout << "4: Wizard\n";
cout << ">";
cin >> choice;

switch (choice)
{
case 1:
{
cout << "\nYou have chosen to be Cleric.\n";
player.SetCharClass(1);
break;
}
case 2:
{
cout << "\nYou have chosen to be Fighter.\n";
player.SetCharClass(2);
break;
}
case 3:
{
cout << "\nYou have chosen to be Rougue.\n";
player.SetCharClass(3);
break;
}
case 4:
{
cout << "\nYou have chosen to be Wizard.\n";
player.SetCharClass(4);
break;
}
default: break;
}
}


return true;
}

stdafx.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once
#ifndef GLOBAL_DEFINE
#define GLOBAL_DEFINE

//#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include "iostream"
#include "stdio.h"
#include "Console.h"
#include "Entity.h"
#include "Item.h"
#include "Potion.h"
#include "Armor.h"
#include "Weapon.h"
#include "Player.h"



bool CreateChar();

using namespace std;
namespace con = JadedHoboConsole;

#endif

// TODO: reference additional headers your program requires here



Visual C++12  
 
 
Brian Kramer





PostPosted: Visual C++ Language, Class help Top

player.h is being included twice in the compilation of player.cpp. The first inclusion is via stdafx.h, and the second is direct.

This in itself is okay, but you need to ensure that the definitions in player.h only occur once. This is done by adding "include guards" in your header.

At the top of player.h:

#ifndef __PLAYER_H__
#define __PLAYER_H__

At the bottom of player.h

#endif // __PLAYER_H__

I do this for all of my own headers... standard practice.

Brian


 
 
Little_Dice





PostPosted: Visual C++ Language, Class help Top

So do i put the #ifndef __PLAYER_H__ and #define __PLAYER_H__ at the begining of everything and #endif // __PLAYER_H__ at the end of everything (even the };)
Also
guards.h isn't found in my library and where would i put it. Sorry im new at using classes.

 
 
Marius Bancila





PostPosted: Visual C++ Language, Class help Top

See this FAQ about guards.

 
 
Brian Kramer





PostPosted: Visual C++ Language, Class help Top

Not the beginning and ending of everything, only as I stated.  For other headers, choose a differently-named header (reusing __PLAYER_H__ defeats the purpose.)  I didn't say anything about guard.h.  Also, this issue isn't specific to classes.  It can occur for any C or C++ definition.

Nice FAQ, Marius.  Thanks.  (Also for the hex conversion thread.)

PS A recent example of a forum poster using an include guard: http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=331570&SiteID=1

 


 
 
Bite Qiu - MSFT





PostPosted: Visual C++ Language, Class help Top

Hi, Little_Dice

what Brian and Marius recommended is good programming practice (not mandatory to do so) to keep you away from same kind of problem ever. You just simply to make sure all your header file has following structure:

// xyz.h

#ifndef __XYZ_H__

#define __XYZ_H__

// all your header file content.

#endif // __XYZ_H__

then you can get rid of your problem. Use this structure to avoid multi-include header file has a term called "include guard", just like Brian and Marius mentioned. furthermore, there is no need to keep your guard something like __x_x_, it could be any string that valid to be a macro.

Thanks

Bite



 
 
Brian Kramer





PostPosted: Visual C++ Language, Class help Top

(I seem to be hearing an echo.) Bite, MVP's are elected because Microsoft feels that they can hold their own in representing Microsoft products, and allows money to be spent on employees doing other things. :)
 
 
Little_Dice





PostPosted: Visual C++ Language, Class help Top

Thanks for all of the suggestions. I have tried all of them but I am still getting the same errors. Any ideas