Yay for errors and headaches

Started by
4 comments, last by metimmee 16 years, 11 months ago
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]
Advertisement
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

// 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)
Quote:
class cPlayer;


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

class SomeClass
{

}; // <- here
Works now!! Thanks you so much!
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 okvoid 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   //..   //..  }}}

This topic is closed to new replies.

Advertisement