Thread: Simple OOP help

  1. #1
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76

    Simple OOP help

    I'm in the process of learning OOP, as my "teacher/link referencer" told me was the best thing to learn next. I started working on a simple T-RPG to put the concepts to use, while learning. But I made a function that couts "Hi!" and it gives an error(could not execute function without object), when ad an object it scolds me for not using it(folowing another tip from my "teacher", to make all warnings errors)

    <b>main.cpp</b>
    Code:
    #include <iostream>
    #include "player.h"
    
    using namespace std;
    
    int main()
    {
        hero::say_hi();
    }
    <b>player.cpp</b>
    Code:
    #include <iostream>
    #include "player.h"
    
    using namespace std;
    
    char hero::namehero(){
        //To get desired name
        cin.get();
    }
    void hero::say_hi(){cout<<"Hi!";}
    <b>player.h</b>
    Code:
    #ifndef PLAYER_H_INCLUDED
    #define PLAYER_H_INCLUDED
    
    #endif // PLAYER_H_INCLUDED
    class hero{
        public:
        /** Set/Get functions **/
            char namehero();
            void say_hi();
        /** End of Set/Get functions **/
        private:
        /** Needed variables **/
            char hero_name;
        /** End of variables list **/
    };
    If you could help me find the error it would greatly help as I am befowled at the problem at hand.(my guess is that it's really obvious)

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A class is a blueprint for an instance of an object. You need to create an instance of the object, then call the method on the created instance.

    Code:
    int main(void)
    {
        int j; // created an instance of an integer named j
    }

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Yog Sothoth!

    "OOP" is garbage umbrella term that can mean damn near anything and everything.

    Your "teacher" was wrong.

    You need to learn to program in C++. You need to learn to use the facilities the C++ standard library provides. Don't try to bite off more than you can chew by trying to incorporate "OOP", functional programming, generic programming, or whatever else into your source until you learn the basics. You'll drown in details.

    The `std::istream::get()' method returns an `int' value. You are doing nothing with that value.

    The `hero::namehero' calls for a `char' value to be returned. You are not returning any value from that method.

    Using class methods naturally requires an instance of the class in question. You don't have an instance of the `hero' class.

    The "header guards" must enclose the entire header. You are only enclosing the guard definition.

    Soma

  4. #4
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Quote Originally Posted by phantomotap View Post
    Yog Sothoth!

    "OOP" is garbage umbrella term that can mean damn near anything and everything.

    Your "teacher" was wrong.

    You need to learn to program in C++. You need to learn to use the facilities the C++ standard library provides. Don't try to bite off more than you can chew by trying to incorporate "OOP", functional programming, generic programming, or whatever else into your source until you learn the basics. You'll drown in details.

    The `std::istream::get()' method returns an `int' value. You are doing nothing with that value.
    I know that, I was tryig to go one problem at a time(one sample function to test if everything was working(hero::say_hi))then i wasn't able to fix it so I wrote a few more lines(but not the full function(hero::namehero))
    Quote Originally Posted by phantomotap View Post

    The `hero::namehero' calls for a `char' value to be returned. You are not returning any value from that method.
    Again,I know I am making one thing at a time
    Quote Originally Posted by phantomotap View Post

    Using class methods naturally requires an instance of the class in question. You don't have an instance of the `hero' class.
    I'm guessing by your reply you know how to work with OOP, Could you teach me how to use it/give me a link to learn from?
    Quote Originally Posted by phantomotap View Post

    The "header guards" must enclose the entire header. You are only enclosing the guard definition.
    Not sure what you mean, there
    Quote Originally Posted by phantomotap View Post

    Soma

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Again,I know I am making one thing at a time
    I find this unlikely as I find it unlikely that you expect the name of a hero to be one character.

    It is very likely that you should be using `std::string'.

    I'm guessing by your reply you know how to work with OOP, Could you teach me how to use it/give me a link to learn from?
    Understanding how to use the facilities a language provides is not "OOP". It is simply good programming. (Contrast this with constantly reimplementing or ignoring the facilities a language provides.)

    I'm sure that if you search for "C++ using classes" you'll find some examples.

    Not sure what you mean, there
    The way you have "implemented" the header you will still get multiple definition errors if the happens to be included twice in any translation unit.

    Soma

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Move:
    Code:
    #endif // PLAYER_H_INCLUDED
    to the end and create an instance of a hero E.g.
    Code:
    hero superman;
    superman.say_hi();
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Alright, I'll try that!

    Edit: once Code::blocks is instaled on this computer
    Last edited by jerimo; 07-16-2010 at 08:55 PM. Reason: for clarification

  8. #8
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Sorry for double post but...

    IT WORKS!!! Thanks alot,Imalc!

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    You could probably change the namehero() function to this:

    Code:
    #include <string>
    
    //...
    void hero::namehero(){
        cout<< "Please input the name of your hero." <<endl;
        //Get desired name
        cin>>hero_name;
    }
    Note that the private "hero_name" variable needs to be changed to a std::string instead of a char.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    How about:

    Code:
    void SetHeroName(const std::string &heroName)
    {
       m_heroName = heroName;
    }
    As opposed to tieing the set function to an input stream.

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Bubba View Post
    As opposed to tieing the set function to an input stream.
    And you could probably write "tying" as opposed to "tieing".
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Hehe. Originally I had it as tying but it didn't look right so I switched it to tieing. One of those cases where neither word looked correct to me.

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Well, its more commonly known as "tying", though I guess so many people mis-wrote/write it as "tieing", it eventually became a word too:
    tieing - Definition of tieing at YourDictionary.com

    I still think "tying" looks better, though I guess its a matter of personal preference.
    Last edited by Programmer_P; 07-17-2010 at 01:18 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Programmer_P View Post
    You could probably change the namehero() function to this:

    Code:
    #include <string>
    
    //...
    void hero::namehero(){
        cout<< "Please input the name of your hero." <<endl;
        //Get desired name
        cin>>hero_name;
    }
    Note that the private "hero_name" variable needs to be changed to a std::string instead of a char.
    Err, this breaks flexibility. What if you don't want the name to come from the input?
    Don't put such things inside classes. It is better for them to be as abstract as possible and controlled via the main code. Your code says that to name a hero, the hero ask for a name and names him/herself. Not what I'd call the definition of naming. When naming someone, you give them a name.
    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.

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Err, this breaks flexibility. What if you don't want the name to come from the input?
    Don't put such things inside classes. It is better for them to be as abstract as possible and controlled via the main code. Your code says that to name a hero, the hero ask for a name and names him/herself. Not what I'd call the definition of naming. When naming someone, you give them a name.
    Well, apparently the OP does, since he's using cin inside his member function.
    I was just demonstrating how to make that work, I wasn't suggesting that that was the best way to do things. Obviously its not.
    And for the record, the hero isn't asking for a name. The function namehero() is. Don't confuse the programmer writing and reading the code with the user of the program, who is only running it. From the user's side, all he would be seeing is the statement to input the name of the hero.
    And the user would input the name of his/her's hero.
    Last edited by Programmer_P; 07-17-2010 at 03:51 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. OOP flu
    By Hussain Hani in forum General Discussions
    Replies: 15
    Last Post: 06-27-2009, 02:02 AM
  3. Should OOP be any new language priority??
    By Hussain Hani in forum General Discussions
    Replies: 80
    Last Post: 06-13-2009, 10:56 AM
  4. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

Tags for this Thread