I see. Right. Okay then. No harm done.
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
Like you were told, move the red line to the end of the file.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 **/ };
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.
You're still not creating an object of the "hero" class. You need to create an object, like this:
in int main(). Nor did you apparently follow any of the other advice in this thread.Code:hero someHero; //you now have an object of said class someHero.say_hi(); //the "hero" says hi
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.
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
Umm...actually it is.
player.h from the posted .txt (changed to a .rar) file download link I'm quoting:
You have read your own files, right?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 **/ };
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.
Wow! You gave up that fight quickly.I see. Right. Okay then. No harm done.
main.cpp
player.hCode:#include <iostream> #include "regions.h" #include "player.h" using namespace std; int main() { build.area(1); build.area(2); }
player.cppCode:#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
regions.hCode:#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.cppCode:#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
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"); } }
>>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.
As stated in the very first reply, which seems to have been completely ignored.
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?)
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.