Sign in to follow this  
finky45

Yay for errors and headaches

Recommended Posts

finky45    100
I'm trying to practice using classes so I'm working on a game. 2 hrs in, I'm stuck... -----------------------------------------------------
// cPlayer.h

#include <iostream>
#include <string>

using namespace std;

class cPlayer
{
public:
       cPlayer (string, int);
//       -cPlayer(){}
       
       int getHealth();
       int getMoney();
       void setHealth(int);
       void setMoney(int);
private:
       int signed itsHealth;
       int itsMoney;
       string itsName;
}
------------------------------------------------------------
// cPlayer.cpp

#include "cPlayer.h"

cPlayer::cPlayer(string name, int money)
{
     itsHealth=100;
     itsMoney=money;
     itsName=name;
}

int getHealth()
{
    return itsHealth;
}
int getMoney()
{
    return itsMoney;
}
void setHealth(int health)
{
     itsHealth=health;
}
void setMoney(int money)
{
     itsMoney+=money;
}
----------------------------------------------------------- ERRORS: cPlayer.cpp:6: error: new types may not be defined in a return type cPlayer.cpp:6: error: return type specification for constructor invalid cPlayer.cpp: In function `int getHealth()': cPlayer.cpp:14: error: `itsHealth' undeclared (first use this function) cPlayer.cpp:14: error: (Each undeclared identifier is reported only once for each function it appears in.) cPlayer.cpp: In function `int getMoney()': cPlayer.cpp:18: error: `itsMoney' undeclared (first use this function) cPlayer.cpp: In function `void setHealth(int)': cPlayer.cpp:22: error: `itsHealth' undeclared (first use this function) cPlayer.cpp: In function `void setMoney(int)': cPlayer.cpp:26: error: `itsMoney' undeclared (first use this function) "cPlayer.cpp:6: error: new types may not be defined in a return type cPlayer.cpp:6: error: return type specification for constructor invalid" I thought constructors didnt have return type... I'm so confused now, and my head hurts. [Edited by - finky45 on May 6, 2007 5:45:37 PM]

Share this post


Link to post
Share on other sites
Washu    7829
1. [ source ] tags are your friend.
2. You forgot a ; at the end of your class decleration
3. Your other methods in your .cpp file need to be qualified by the class name.

Share this post


Link to post
Share on other sites
finky45    100
// cPlayer.h

#include <iostream>
#include <string>

using namespace std;

class cPlayer;
{
public:
cPlayer (string, int);
// -cPlayer(){}

int getHealth();
int getMoney();
void setHealth(int);
void setMoney(int);
private:
int signed itsHealth;
int itsMoney;
string itsName;
}


// cPlayer.cpp

#include "cPlayer.h"

cPlayer::cPlayer(string name, int money)
{
itsHealth=100;
itsMoney=money;
itsName=name;
}

int cPlayer::getHealth()
{
return itsHealth;
}
int cPlayer::getMoney()
{
return itsMoney;
}
void cPlayer::setHealth(int health)
{
itsHealth=health;
}
void cPlayer::setMoney(int money)
{
itsMoney+=money;
}





Still gettin errors:

cPlayer.h:9: error: expected unqualified-id before '{' token
cPlayer.h:9: error: expected `,' or `;' before '{' token
cPlayer.cpp:6: error: invalid use of undefined type `struct cPlayer'
cPlayer.h:8: error: forward declaration of `struct cPlayer'
cPlayer.cpp: In constructor `cPlayer::cPlayer(std::string, int)':
cPlayer.cpp:7: error: `itsHealth' undeclared (first use this function)
cPlayer.cpp:7: error: (Each undeclared identifier is reported only once for each function it appears in.)

cPlayer.cpp:8: error: `itsMoney' undeclared (first use this function)
cPlayer.cpp:9: error: `itsName' undeclared (first use this function)

cPlayer.cpp: At global scope:
cPlayer.cpp:13: error: invalid use of undefined type `struct cPlayer'
cPlayer.h:8: error: forward declaration of `struct cPlayer'
cPlayer.cpp: In member function `int cPlayer::getHealth()':

cPlayer.cpp:14: error: `itsHealth' undeclared (first use this function)
cPlayer.cpp: At global scope:
cPlayer.cpp:17: error: invalid use of undefined type `struct cPlayer'
cPlayer.h:8: error: forward declaration of `struct cPlayer'
cPlayer.cpp: In member function `int cPlayer::getMoney()':
cPlayer.cpp:18: error: `itsMoney' undeclared (first use this function)

cPlayer.cpp: At global scope:
cPlayer.cpp:21: error: invalid use of undefined type `struct cPlayer'
cPlayer.h:8: error: forward declaration of `struct cPlayer'

cPlayer.cpp: In member function `void cPlayer::setHealth(int)':
cPlayer.cpp:22: error: `itsHealth' undeclared (first use this function)
cPlayer.cpp: At global scope:
cPlayer.cpp:25: error: invalid use of undefined type `struct cPlayer'

cPlayer.h:8: error: forward declaration of `struct cPlayer'
cPlayer.cpp: In member function `void cPlayer::setMoney(int)':
cPlayer.cpp:26: error: `itsMoney' undeclared (first use this function)

Share this post


Link to post
Share on other sites
Scet    960
Quote:

class cPlayer;


No, on the end of the class, past the closing bracket.

class SomeClass
{

}; // <- here

Share this post


Link to post
Share on other sites
metimmee    128
As a side-note, avoid the 'using' keyword in .h since any subsequent inclusion of that .h will also inherit the 'using namespace std'.

Using within a cpp is better since the scope is immediately limited, although that can be improved upon.



//this, in the most part will be ok
void CMyClass::MyFunc()
{
using namespace std;

string myString....
//lots of namespace std stuff
//..
//..
}

//By limiting the scope of the using keyword, you can avoid potential
// clashes later.
void CMyClass::MyFunc()
{

{//You can limit the scope like this
using namespace std;
string myString....
//lots of namespace std stuff
//..
//..
}

{
using namespace widget;
string myWidgetString ....
//lots of namespace widget stuff
//..
//..
}
}

}




Share this post


Link to post
Share on other sites

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