Jump to content
  • Advertisement
Sign in to follow this  
finky45

Yay for errors and headaches

This topic is 4066 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

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
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.

Share this post


Link to post
Share on other sites
// 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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!