#### Archived

This topic is now archived and is closed to further replies.

# Am I missing something?

## Recommended Posts

Got quite an odd problem (at least it appears to be, on the surface). Here's the source, firstly: player.h
// player.h

// Definition of class Player.

#ifndef PLAYER_H
#define PLAYER_H

class CPlayer
{
public:
CPlayer(int = 100, char * = "Player 1"); // Constructor.

~CPlayer();                              // Destructor.

int getHealth() const;
char *getPlayerName() const;
private:
int health;
char *playerName;
};

#endif

player.cpp
// player.cpp

// Member function definitions for class Player.

#include <cstring>
#include <cassert>
#include "player.h"

CPlayer::CPlayer(int hp, char *pName)
{
health = (hp > 0 && hp <= 100) ? hp : 100;

playerName = new char[strlen(pName) + 1];
assert(playerName != 0); // For simplicity's sake.  Rem later.

strcpy(playerName, pName);
}

CPlayer::~CPlayer()
{
delete [] playerName;
}

int CPlayer::getHealth() const { return health; }
char *CPlayer::getPlayerName() const { return playerName; }

and lastly, 2 driver programs...
// This version works.

#include <iostream>
#include "player.h"

using std::cout;

int main()
{
CPlayer *Player1 = new CPlayer();

cout << "Player: " << Player1->getPlayerName()
<< "\nHealth: " << Player1->getHealth()
<< "\n";

delete Player1;

return 0;
}

// This version doesn't.

#include <iostream>
#include "player.h"

using std::cout;

int main()
{
CPlayer Player1();

cout << "Player: " << Player1.getPlayerName()   // line 13

<< "\nHealth: " << Player1.getHealth() // line 14

<< "\n";

return 0;
}

I can't see anything particularly wrong with the second driver program, but it's as if the Player1 class gets completely ignored. Here's the errors: Compiling... main.cpp d:\classes\player\main.cpp(13) : error C2228: left of '.getPlayerName' must have class/struct/union type d:\classes\player\main.cpp(14) : error C2228: left of '.getHealth' must have class/struct/union type Error executing cl.exe. player.exe - 2 error(s), 0 warning(s) I know the problem is that the constructor isn't being called, but why exactly is that? As you can see from the class definition, defaults have been set for both parameters in the constructor. Shouldn't that mean that empty parenthesis call the constructor as normal? Any information would be appreciated. Edit: Oops, phrased that wrong. I know the class is being "ignored", as I put it, because it's not being constructed. --hellz [edited by - hellz on September 1, 2003 10:59:01 AM]

##### Share on other sites
Maybe leave out the parentheses when declaring a local object with the default constructor? Old versions of VC++ used to give me problems with that. I'm not sure what the standard says about it. So your second driver prog should look like...

#include <iostream>#include "player.h"using std::cout;int main(){	  CPlayer Player1; // note: no parentheses	  cout << "Player: " << Player1.getPlayerName()   // line 13        << "\nHealth: " << Player1.getHealth() // line 14	<< "\n";	  return 0;}

[edited by - Malone1234 on September 1, 2003 11:05:13 AM]

##### Share on other sites
Well god damn. Thanks a lot! First time I've encountered that one, although I've used empty parenthesis before. Reckon it's a compiler issue or a language issue (so many syntax rules, my poor brain can't cope )?

Thanks again mate.

--hellz

[edited by - hellz on September 1, 2003 11:10:26 AM]

##### Share on other sites
I believe it means something else when you do that (put empty parentheses on a variable declaration), but I can''t remember what. I do remember seeing it done in exception-handling code before.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

##### Share on other sites
Actually, the compiler might see it as a function declaration.

##### Share on other sites
Yeah, that was my guess at first. It just looks strange I guess. I swear I''ve used the notation before though, which is what''s bothering me.

Thanks mate,

--hellz

• ### Forum Statistics

• Total Topics
628385
• Total Posts
2982391

• 10
• 9
• 15
• 24
• 11