Problem with class.

This is a discussion on Problem with class. within the C++ Programming forums, part of the General Programming Boards category; I got a problem with this code. here it is. Show.h Code: //Menu class class Show { public: void CharStats(); ...

  1. #1
    deletedforumuser
    Guest

    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?
    Last edited by kevinawad; 06-26-2008 at 09:21 PM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    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.

  3. #3
    deletedforumuser
    Guest
    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.
    Last edited by kevinawad; 06-26-2008 at 10:12 PM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    deletedforumuser
    Guest
    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?
    Last edited by kevinawad; 06-27-2008 at 07:22 AM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    deletedforumuser
    Guest
    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?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 04:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21