Thread: NewB with C++ - Functions & Classes

  1. #1
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28

    Question NewB with C++ - Functions & Classes

    Hi,

    I'm new to C++ programming. I've used Qbasic in the past, as well as Borland Delphi, but I decided that I'd probably learn something as commenly known as C++

    after reading through the tutorials here, I tried my hand on some of the possibilities, but I got stuck trying to figure functions out. Below is my code, I don't see why it shouldn't work...

    Code:
    #include <iostream>
    
    using namespace std;
    
    class PlayerObj // Standard way of defining the class
    {
    public:
      PlayerObj();
      // Constructor
      ~PlayerObj();
      // Destructor
      void setspeed ( int p );
      int readspeed();
      void Setdir ( int d );
      int Readdir();
      char Textface();
    protected:
    
      int Playerspeed;
      int PlayerDir;
    };
    
    PlayerObj::PlayerObj()
    {
      Playerspeed = 0;
      PlayerDir = 3;
    }
    
    PlayerObj::~PlayerObj()
    {
    	//Destructors do not accept arguments
    }
    
    void PlayerObj::setspeed ( int p )
    {
    
      Playerspeed = p;
    }
    int PlayerObj::readspeed()  
    {
    
      return Playerspeed;
    }
    
    void PlayerObj::Setdir (int d )
    {
    	 
       PlayerDir = d;
    }
    
    int PlayerObj::Readdir()
    {
    	
    	return PlayerDir;
    }
    
    char PlayerObj::Textface()
    {
    // Here's the problem... I've attempted to do it as depicted below this line,
    // as well as the method depicted below that. Neither worked...
    	if ( PlayerDir > 8 ) { Playerdir = 1 ; }
    	if ( Player1.Readdir() == 1 ) return "Up";
    	if ( Player1.Readdir() == 2 ) return "Up + Right";
    	if ( Player1.Readdir() == 3 ) return "Right";
    	if ( Player1.Readdir() == 4 ) return "Right + Down";
    	if ( Player1.Readdir() == 5 ) return "Down"
    	if ( Player1.Readdir() == 6 ) return "Down + Left";
    	if ( Player1.Readdir() == 7 ) return "Left";
    	if ( Player1.Readdir() == 8 ) return "Left + Up;"
     }
    
    int main()
    {
      PlayerObj Player1;  
     
      Player1.setspeed ( 100 ); 
    
      cout<< Player1.readspeed();
      cout<<"\nPlayer1 is facing: "<< Player1.Textface() <<"\n";
      
      cin.get();
    
    }
    I'm probably missing something really obvious, but it doesn't compile telling me that "PlayerDir" is undeclared, or with the other approach, Player1.

    Any help would be appreciated!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Within a class method, you can't refer to the instance that is being used. So inside of Textface you shouldn't be mentioning Player1. Your first attempt was the right idea, but you just need to pay attention to case: Playerdir is different than PlayerDir.

    Also, your Textface method is returning a char. A char holds only a single character, you want a string of characters known in programming simply as a string. The C++ string class is included in <string>. Use that instead of char.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    class PlayerObj // Standard way of defining the class
    {
    public:
      PlayerObj();
      // Constructor
      ~PlayerObj();
      // Destructor
      void setspeed ( int p );
      int readspeed();
      void Setdir ( int d );
      int Readdir();
      char Textface();   // You're trying to return a string, so make this a string
    protected:
    
      int Playerspeed;
      int PlayerDir;
    };
    
    char PlayerObj::Textface()  // This should be a const char*
    {
    // Here's the problem... I've attempted to do it as depicted below this line,
    // as well as the method depicted below that. Neither worked...
    	if ( PlayerDir > 8 ) { Playerdir = 1 ; }  // Syntax Error
    	if ( Player1.Readdir() == 1 ) return "Up";  // Remove the object name, you only want the function name
    	if ( Player1.Readdir() == 2 ) return "Up + Right"; // These are not characters
    	if ( Player1.Readdir() == 3 ) return "Right";
    	if ( Player1.Readdir() == 4 ) return "Right + Down";
    	if ( Player1.Readdir() == 5 ) return "Down"  // Syntax Error
    	if ( Player1.Readdir() == 6 ) return "Down + Left";
    	if ( Player1.Readdir() == 7 ) return "Left";
    	if ( Player1.Readdir() == 8 ) return "Left + Up;"  // Syntax Error
     }
    [/QUOTE]
    Last edited by SlyMaelstrom; 11-14-2005 at 05:56 PM.
    Sent from my iPad®

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Problem one, char's only hold one character.
    Code:
    #include <string>
    And make that function
    Code:
     string Textface();
    And fix the defination as well.

    This line:
    Code:
    	if ( PlayerDir > 8 ) { Playerdir = 1 ; }
    Does not work because the second PlayerDir does not have the D capitalized.


    No, the rest of your if statements in this function.
    Player1? No. You are thinking of object oriented programming all wrong.
    Your class is an object called PlayerObj

    When you declare a object of type player object in main
    Code:
    PlayerObj player1
    player1 is now that object. You can not in your object definition (the class) specify a specific object name.
    Uhh…if that makes sense. Just remove the player1. part of all those lines (and a few of them are missing the ; at the end.

    *edit* Beat while typing!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Some other comments:

    Code:
    void setspeed ( int p );
    int readspeed();
    void Setdir ( int d );
    int Readdir();
    Inconsistency in the capitalization here can be confusing for other potential users of your class (and even yourself perhaps). If other people have to use this class, they might stumble on why the 'r' in readspeed is lowercase and why the 'R' in Readdir is uppercase. I would suggest you pick a naming convention to use throughout the class and stick to it. You even have differences in the capitalization of the 'd' between the Setdir and Readdir member functions and the PlayerDir member variable... very confusing.



    Code:
    if ( PlayerDir > 8 ) { PlayerDir = 1 ; }
    if ( Player1.Readdir() == 1 ) return "Up";
    if ( Player1.Readdir() == 2 ) return "Up + Right";
    if ( Player1.Readdir() == 3 ) return "Right";
    if ( Player1.Readdir() == 4 ) return "Right + Down";
    if ( Player1.Readdir() == 5 ) return "Down"
    if ( Player1.Readdir() == 6 ) return "Down + Left";
    if ( Player1.Readdir() == 7 ) return "Left";
    if ( Player1.Readdir() == 8 ) return "Left + Up;"
    Again there is a matter of consistency, why directly access the PlayerDir value in the first line there and then turn around and use the Readdir functions on every other line to access the value. I'd personally stick to just directly accessing the value to save on the overhead of the function call. Also, there is the matter of the validation check of the PlayerDir value in the first line. Since this value is protected (should be private unless you plan on doing some inheritance with the class later on) and is only set via the Setdir member function (other than in the constructor), then it would seem that the Setdir function should be the place to validate and reset that:
    Code:
    void PlayerObj::Setdir (int d )
    {
        if( d > 8 ) d = 1;
        PlayerDir = d;
    }
    
    string PlayerObj::Textface()
    {
        if ( PlayerDir == 1 ) return "Up";
        else if ( PlayerDir == 2 ) return "Up + Right";
        else if ( PlayerDir == 3 ) return "Right";
        else if ( PlayerDir == 4 ) return "Right + Down";
        else if ( PlayerDir == 5 ) return "Down"
        else if ( PlayerDir == 6 ) return "Down + Left";
        else if ( PlayerDir == 7 ) return "Left";
        else return "Left + Up;"
     }
    Just my thoughts on the matter.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28
    Thanks everyone, that cleared the whole bunch up

    By the way, Would anyone know how to get some simple figures on the screen without having to download the 212MB DirectX SDK?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Rider
    Thanks everyone, that cleared the whole bunch up

    By the way, Would anyone know how to get some simple figures on the screen without having to download the 212MB DirectX SDK?
    You can use the GDI function just as TextOut() function. You will need to create a windows API program with a window to diaply the text on.
    Last edited by Quantum1024; 11-15-2005 at 04:08 PM.

  8. #8
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28
    Quote Originally Posted by Quantum1024
    You can use the GDI function just as TextOut() function. You will need to create a windows API program with a window to diaply the text on.
    Would this allow me to create simple forms and shapes besides just plain text?

    How would I create a windows API program, and window?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The first thing you need is a windows compiler, like Dev-C++.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> Would anyone know how to get some simple figures on the screen without having to download the 212MB DirectX SDK?

    check out sdl.. lots of fun

    http://www.libsdl.org

  11. #11
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28

    Derail to SDL

    Quote Originally Posted by xhi
    >> Would anyone know how to get some simple figures on the screen without having to download the 212MB DirectX SDK?

    check out sdl.. lots of fun

    http://www.libsdl.org
    It looks allright, but I can't seem to figure out how to get the package running in Dev-C++ once I downloaded it.

    It seems like I have all the files, I just don't know what to do with 'em...

  12. #12
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You usually need to change the project options to include the extra libraries. But I'm sure that's in the link provided.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User Rider's Avatar
    Join Date
    Nov 2005
    Posts
    28
    It's kind of difficult to get the right bits of information, especially when several sites tell me varying things about how to get SDL to work.

    I run Windows 2000.

    My Dev-C++ is installed in: C:\Devtools\Dev-cpp
    I added the libraries as explained in the link above.
    I've moved the DLL's to my new project's directory (My documents\Code 'n such\Tutorials\Tstprj)

    but compiling the code from THIS tutorial, doesn't work. First the complier tells me that SDL.h can't be found, setting up the #include to include to full path of that file, seems to work...

    but then I get another compiler error, namely "cannot find -lSDLmain"

    What's wrong, and what do I need to fix before it'll just be nice and compile?

  15. #15
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Code:
    #if defined(_MSC_VER)
    #include "SDL.h"
    #else
    #include "SDL/SDL.h"
    #endif
    Assuming thats the code you were using when you tried to compile..

    If you have put the SDL includes in the include path by doing this

    >> Create an \SDL directory in the \include subdirectory of your Dev-C++ installation root, and copy all of the SDL header files into this new directory.

    then you should not be quoting the includes.. it should be

    <SDL/SDL.h>

    one thing to watch is that some older tutorials show it as

    <SDL.h>

    the newer versions of SDL have changed to putting SDL in its own dir under the include path, so you should do like I mentioned before and use

    #include <SDL/SDL.h>

    As for the libs error.. did you do this
    >> Copy SDL.dll and libSDLmain.a from the SDL library into the \lib subdirectory of your Dev-C++ installation root.

    and

    >> Click the Parameters tab, and in the Linker textbox enter "-lmingw32 -lSDLmain -lSDL -lSDL_mixer" (without the quotes) then click Ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of classes?
    By dream_noir in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2009, 11:43 AM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Beginner Classes Question
    By kbro3 in forum C++ Programming
    Replies: 9
    Last Post: 08-14-2008, 07:43 AM
  4. Classes and member functions
    By uraliss in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2006, 07:38 AM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM