-
Problem with class.
I got a problem with this code. here it is.
Show.h
Code:
//Menu class
class Show
{
public:
void CharStats();
void WhatToDo();
private:
};
Show.cpp
Code:
#include "Library.h"
//Char stats
void Show::CharStats()
{
cout <<"Name: "<<player.getName();
}
Player.h
Code:
//Player class
class Player
{
public:
string getName();
void setName(string playername);
private:
string pName;
};
Player.cpp
Code:
#include "Library.h"
//Player Name
string Player::getName()
{
return pName;
}
void Player::setName(string playername)
{
pName = playername;
}
Okay, now, in main.cpp, i have created the class(the object).
I've written this:
Code:
//Create the player
Player player;
Now, what im trying to do is acessing it from the Show.cpp (Show class) function called charstats.
The charstats will actually print the player.getName() on the screen...How am i able to acess player.getName from there?
-
Pass the Player object as a parameter to the Show::CharStats() function, but first, Google for "Thinking in C++" and download a wonderful, free, C++ book.
-
Can you show me how please? Thank you.
And by the way, im reading right now, thinking in c++. Thanks for the free book!.
-Edit: Can anyone help me resolve this please? Thank you.
-
Like this?
Code:
void Show::CharStats(Player player)
{
cout <<"Name: "<<player.getName();
}
It wouldn't hurt to read about const correctness (e.g Player::getName should be a const method). Then you could use the preferred signature:
Code:
void Show::CharStats(const Player& player)
{
cout <<"Name: "<<player.getName();
}
Your practice of an include-all header also seems a bit dubious. Generally each file should include only those headers that it absolutely needs to. I imagine that an include-all header would lead to cyclic inclusion problems, and it can't be good having to recompile every file in the project as soon as any of the headers changes - even if they don't use anything from that header at all.
-
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
And ummm, it isn't good to include everything in one library?
And why does it need to be a const?
-
Const can have two (three) functions:
1) It helps you find bugs - because you cannot modify something that is const.
2) It helps the compiler with optimizations, because if something is const, the compiler can assume that it will never be modified.
(3)) Const references can be bound to temporaries. That means you can pass "my string" to a function expecting const std::string&, without problems. If the function expected simply std::string&, it wouldn't work if you didn't pass a real std::string variable.
Therefore, apply const to everything that shouldn't change.
-
Okay, thanks, so let's say, i want to set the window_height to 480.
And, i will never change it, it will stay like that forever, i need to put const before? will it be better?
-
Yes.
Another good book you should pick up is the C++ FAQs by Cline, Lomow, and Girou. One read through that would answer a lot of the questions you have. It's a great reference, too.