Simple OOP help

This is a discussion on Simple OOP help within the C++ Programming forums, part of the General Programming Boards category; I see. Right. Okay then. No harm done....

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    I see. Right. Okay then. No harm done.
    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.

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

    little more help needed

    I ran into another problem, but I'm not sure id=f it's really the code or if its' code blocks 10.05.

    Code:
    obj\Debug\player.o:J:\C++\[OOP]T-RPG\player.cpp|9|multiple definition of `hero'|
    obj\Debug\main.o:c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iostream|72|first defined here|
    obj\Debug\regions.o||In function `_tcf_0':|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iostream|72|multiple definition of `home'|
    obj\Debug\main.o:c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iostream|72|first defined here|
    obj\Debug\regions.o||In function `_tcf_0':|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iostream|72|multiple definition of `lawn'|
    obj\Debug\main.o:c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iostream|72|first defined here|
    obj\Debug\regions.o:J:\C++\[OOP]T-RPG\regions.cpp|12|multiple definition of `build'|
    obj\Debug\main.o:J:\C++\[OOP]T-RPG\main.cpp|14|first defined here|
    obj\Debug\initialiser.o||In function `GLOBAL__I_home':|
    J:\C++\[OOP]T-RPG\regions.h|7|multiple definition of `home'|
    obj\Debug\main.o:c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iostream|72|first defined here|
    obj\Debug\initialiser.o||In function `_static_initialization_and_destruction_0':|
    J:\C++\[OOP]T-RPG\regions.h|25|multiple definition of `lawn'|
    obj\Debug\main.o:c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iostream|72|first defined here|
    obj\Debug\initialiser.o||In function `_static_initialization_and_destruction_0':|
    J:\C++\[OOP]T-RPG\regions.h|25|multiple definition of `hero'|
    obj\Debug\main.o:c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\iostream|72|first defined here|
    obj\Debug\initialiser.o||In function `_static_initialization_and_destruction_0':|
    J:\C++\[OOP]T-RPG\regions.h|25|multiple definition of `build'|
    obj\Debug\main.o:J:\C++\[OOP]T-RPG\main.cpp|14|first defined here|
    ||=== Build finished: 16 errors, 0 warnings ===|

    the joined file is a .rar, but we can't upload those so you'll have to rename it to .rar
    Attached Files Attached Files

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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 **/
    };
    Like you were told, move the red line to the end of the file.

    And you still have basically addressed none of the points which were raised in the topic. Several people have explained to you what you've done wrong, but the problematic code is still in your source.
    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.

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    You're still not creating an object of the "hero" class. You need to create an object, like this:

    Code:
    hero someHero; //you now have an object of said class
    someHero.say_hi(); //the "hero" says hi
    in int main(). Nor did you apparently follow any of the other advice in this thread.
    Last edited by Programmer_P; 07-17-2010 at 04:03 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #20
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Quote Originally Posted by Elysia View Post
    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 **/
    };
    Like you were told, move the red line to the end of the file.

    And you still have basically addressed none of the points which were raised in the topic. Several people have explained to you what you've done wrong, but the problematic code is still in your source.
    Where did you get that from, the file linked to is not like that!


    EDIT: just go here
    http://cboard.cprogramming.com/attac...op%5Dt-rpg-txt
    Last edited by jerimo; 07-17-2010 at 04:43 PM. Reason: refresher for the link

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jerimo View Post
    Where did you get that from, the file linked to is not like that!

    EDIT: just go here
    http://cboard.cprogramming.com/attac...op%5Dt-rpg-txt
    Umm...actually it is.

    player.h from the posted .txt (changed to a .rar) file download link I'm quoting:

    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 **/
    
    };
    You have read your own files, right?
    Last edited by Programmer_P; 07-17-2010 at 05:42 PM. Reason: none of your business
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #22
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I see. Right. Okay then. No harm done.
    Wow! You gave up that fight quickly.

  8. #23
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Quote Originally Posted by Programmer_P View Post
    Umm...actually it is.

    player.h from the posted .txt (changed to a .rar) file download link I'm quoting:

    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 **/
    
    };
    You have read your own files, right?
    I have read them, and double check after the post, but it's not like that.
    Also, say_hi() doesn't exist anymore!

    I'll just update the OP, in a few

    EDIT: can't edit the OP so will make another post, with code
    Last edited by jerimo; 07-18-2010 at 05:41 PM.

  9. #24
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    main.cpp
    Code:
    #include <iostream>
    #include "regions.h"
    #include "player.h"
    
    using namespace std;
    
    int main()
    {
        build.area(1);
        build.area(2);
    }
    player.h
    Code:
    #ifndef PLAYER_H_INCLUDED
    #define PLAYER_H_INCLUDED
    
    #include <string>
    
    using namespace std;
    
    class character{
        public:
        /** Set functions **/
            void new_player();
            void set_name(string name);
            void set_location(int new_loc);
        /** Get functions **/
            string get_name();
            int get_location();
    
        private:
        /** Start of variable list **/
            string hero_name;
            int hero_location;
        /** End of variables list **/
    };
    
    /** To be able to use the functions **/
    character hero;
    
    class initialise_player{
        public:
            void new_player();
            void choose_name();
        private:
            string name;
    };
    #endif // PLAYER_H_INCLUDED
    player.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "player.h"
    
    using namespace std;
    /** Character class **/
        /** Set functions **/
        void character::set_name(string name){hero_name = name;}
        void character::set_location(int new_loc){hero_location = new_loc;}
    
        /** Get functions **/
        string character::get_name(){return hero_name;}
        int character::get_location(){return hero_location;}
    
    /** Player initialiser **/
        /** New player (Set) **/
        void initialise_player::choose_name(){
    
        }
        void initialise_player::new_player(){
            hero.set_name(name);
            hero.set_location(1);
        }
    regions.h
    Code:
    #ifndef REGIONS_H_INCLUDED
    #define REGIONS_H_INCLUDED
    
    #include <string>
    
    using namespace std;
    class region{
        public:
        /** Set functions **/
            void set_desc(string to_set);
            void set_name(string to_set);
        /** Get functions **/
            string get_desc();
            string get_name();
        /** Data showing **/
            void display(string data);
            void display(int data);
        private:
            int area_ID;
            string area_name;
            string description;
    };
    
    /** To be able to use the functions **/
    region home;
    region lawn;
    
    class initialise_area{
        public:
            void area(float ID);
        private:
    };
    initialise_area build;
    
    #endif // REGIONS_H_INCLUDED
    regions.cpp
    Code:
    #include <iostream>
    #include "regions.h"
    
    /** Set **/
    void region::set_desc(string to_set){description = to_set;}
    void region::set_name(string to_set){area_name = to_set;}
    /** Get **/
    string region::get_desc(){return description;}
    string region::get_name(){return area_name;}
    /** Show **/
    void region::display(string data){cout << data;}
    void region::display(int data){cout << data;}
    
    void initialise_area::area(float ID){
        if(ID == 1){
            home.set_name("Home\n");
            home.set_desc("There's no place like home!\n");
        }
        else if(ID == 2){
            lawn.set_name("Lawn\n");
            lawn.set_desc("The front lawn of your house. You can see the kids playing in the park, on the other side of the road, during the day.\n");
        }
    }

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    >>character hero;
    You are misunderstanding the concepts of objects. Classes aren't just namespaces. They are blueprints. You create instances of those blueprints, known as objects, as you need them. There isn't just a hero, there may be several. Just like there isn't one car, there are a huge amount of cars.

    So don't just create some generic object in a header! And don't create a useless class for the sole purpose of initializing said object!
    Instead create a hero in main, in your code that controls the logic of the program, and then create functions in the hero class that can set its properties and set those properties in said logic.

    Classes are blueprints. Instances of blueprints are objects. Like a real object. You must start thinking like of "real life objects" and how they behave and how you interact with them.
    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.

  11. #26
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    As stated in the very first reply, which seems to have been completely ignored.

  12. #27
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Quote Originally Posted by Elysia View Post
    >>character hero;
    You are misunderstanding the concepts of objects. Classes aren't just namespaces. They are blueprints. You create instances of those blueprints, known as objects, as you need them. There isn't just a hero, there may be several. Just like there isn't one car, there are a huge amount of cars.

    So don't just create some generic object in a header! And don't create a useless class for the sole purpose of initializing said object!
    Instead create a hero in main, in your code that controls the logic of the program, and then create functions in the hero class that can set its properties and set those properties in said logic.

    Classes are blueprints. Instances of blueprints are objects. Like a real object. You must start thinking like of "real life objects" and how they behave and how you interact with them.
    So, I'm supposed to make the first part of main the initialiser

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Yes, do that. There is no sense in creating a new class sorely for initializing another specific instance of a class.
    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.

  14. #29
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    Ok, I just thaught it would be better to initialise them one at a time(but do I do it in main, or before main or maybe both would work?)

  15. #30
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Think of your program as consisting of building blocks.
    For example, in the real world, you have all sorts of utilities. Trucks, cars, TVs, stereos. They all perform certain things, but they don't operate themselves. You need to instruct them what to do.
    Translate that into a program and you get two parts: the utilities, the building blocks and the logic, the code that causes these utilities to do what they must.
    This logic creates objects as necessary (like a company might purchase a car) and then operates that object (like a driver would drive a car).

    The logic begins in main because that's where your program starts. You just need to be able to separate the building blocks from the logic. Anywhere your logic code is, there you can initialize them.
    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.

Page 2 of 2 FirstFirst 12
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, 10: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


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