C++ Workshop - Project 1
dont see why not, but there seemes to be failing interest and feedback from the community :(
Quote:
Original post by linkofazeroth
Even though it's over, I can still try doing it, right?
What would stop you? It's not like we got anything from completing the project other than the knowledge we gained from doing it. You never have to ask to learn.
westond:
I have given your code a little of my time.
One thing that you rely need to do is to use autoformatting or be much more carefull with you indentation.
Visual studio can reformat a complete file.
I had to do some changes to compile the code since I'm using linux and not windows.
If you dont need to include something in a header file please dont. (windows.h in engine.h)
Include it in the .cpp instead. Doing this can reduce compile times alot for large projects.
sleep is a very bad funtion name. I don't think it defined by the c library but it is in the posix standard.
system("pause") is not very portable, so please move it to its own portability layer together with the color code
item.h
why is iostream included here? move to item.cpp
the include guards should be placed before any code, even #include
extern void Text_Color(int color); <--- what is this doing here?
Item do not have any constructor
Set_Item_Type and Get_Item_Type should be virtual if they are overridable or removed from Weapon.
design: why not move name cost and owned to Item.
Weapon should go into its own file.
Set_Weapon_Attributes should be a constructor.
Set_Item_Type and Get_Item_Type should be virtual or removed.
Armor should be moved to its own file.
rpg_main.cpp can be made much shorter with something like this:
Weapon *weaponArray[15];
weaponArray[0]=new Weapon(1,"Unarmed Strike",0,1,1,3,20,2);
weaponArray[1]=new Weapon(1,"Brass Knuckles",10,0,1,4,20,2);
.
.
.
for (int i=0; i< numberOfWeapons; i++) {
delete weaponArray;
}
and break main into many small functions.
I have given your code a little of my time.
One thing that you rely need to do is to use autoformatting or be much more carefull with you indentation.
Visual studio can reformat a complete file.
I had to do some changes to compile the code since I'm using linux and not windows.
If you dont need to include something in a header file please dont. (windows.h in engine.h)
Include it in the .cpp instead. Doing this can reduce compile times alot for large projects.
sleep is a very bad funtion name. I don't think it defined by the c library but it is in the posix standard.
system("pause") is not very portable, so please move it to its own portability layer together with the color code
item.h
why is iostream included here? move to item.cpp
the include guards should be placed before any code, even #include
extern void Text_Color(int color); <--- what is this doing here?
Item do not have any constructor
Set_Item_Type and Get_Item_Type should be virtual if they are overridable or removed from Weapon.
design: why not move name cost and owned to Item.
Weapon should go into its own file.
Set_Weapon_Attributes should be a constructor.
Set_Item_Type and Get_Item_Type should be virtual or removed.
Armor should be moved to its own file.
rpg_main.cpp can be made much shorter with something like this:
Weapon *weaponArray[15];
weaponArray[0]=new Weapon(1,"Unarmed Strike",0,1,1,3,20,2);
weaponArray[1]=new Weapon(1,"Brass Knuckles",10,0,1,4,20,2);
.
.
.
for (int i=0; i< numberOfWeapons; i++) {
delete weaponArray;
}
and break main into many small functions.
I made a 'console class' for ease of coloring text and getting keypresses, and I uploaded it at the URL below. I think it's pretty useful, myself. :D
http://showfile.file.sc/6761/CXq6RdyY/console.htm
EDIT: Click "Download this .h file" in the lower right corner-area. I don't see why they put the ads right in the center of the page... well, maybe I do.
EDIT2: Only for Windows users.
http://showfile.file.sc/6761/CXq6RdyY/console.htm
EDIT: Click "Download this .h file" in the lower right corner-area. I don't see why they put the ads right in the center of the page... well, maybe I do.
EDIT2: Only for Windows users.
Darkstrike
You seems to know your way around c++ :-)
I got problems with:
friend static std::ostream& operator<<(std::ostream& where, MenuOption& what);
according to Effective c++ by Scott Meyers is should be something like:
menu.h
class MenuOption {
public:
MenuOption(char newShortcut, std::string newDescription);
~MenuOption() {};
char getShortcut();
std::string getDescription();
private:
char shortcut;
std::string description;
private:
friend std::ostream& operator<<(std::ostream& where, MenuOption& what);
};
std::ostream& operator<<(std::ostream& where, MenuOption& what);
menu.cpp
std::ostream& operator <<(std::ostream& where, MenuOption& what) {
where << what.getShortcut() << ". " << what.getDescription() << endl;
return where;
}
some of the constructors have this problem:
class foo {
foo(): two(2),one(1) {;}
int one,two;
};
that gives a warning with gcc to remove the warning use this:
class foo {
foo(): one(1),two(2) {;} <--- changed line
int one,two;
};
You seems to know your way around c++ :-)
I got problems with:
friend static std::ostream& operator<<(std::ostream& where, MenuOption& what);
according to Effective c++ by Scott Meyers is should be something like:
menu.h
class MenuOption {
public:
MenuOption(char newShortcut, std::string newDescription);
~MenuOption() {};
char getShortcut();
std::string getDescription();
private:
char shortcut;
std::string description;
private:
friend std::ostream& operator<<(std::ostream& where, MenuOption& what);
};
std::ostream& operator<<(std::ostream& where, MenuOption& what);
menu.cpp
std::ostream& operator <<(std::ostream& where, MenuOption& what) {
where << what.getShortcut() << ". " << what.getDescription() << endl;
return where;
}
some of the constructors have this problem:
class foo {
foo(): two(2),one(1) {;}
int one,two;
};
that gives a warning with gcc to remove the warning use this:
class foo {
foo(): one(1),two(2) {;} <--- changed line
int one,two;
};
Quote:
Original post by me_minus
westond:
I have given your code a little of my time.
One thing that you rely need to do is to use autoformatting or be much more carefull with you indentation.
Visual studio can reformat a complete file.
Thank you so very much. I need all the criticizm I can get. I tried to clean up the indentation as much as possible, VS seemed to chew up most of the formatting. It may just be how I write my code though.
Quote:
If you dont need to include something in a header file please dont. (windows.h in engine.h)
Include it in the .cpp instead. Doing this can reduce compile times alot for large projects.
Is this also the case with user made .h files. If I have a class B in a header and cpp named B but class A in header/cpp file named A that referenced B in some way, then I would have to include the B.h file in the A.h file wouldnt I?
Quote:
sleep is a very bad funtion name. I don't think it defined by the c library but it is in the posix standard.
system("pause") is not very portable, so please move it to its own portability layer together with the color code
I wondered about "sleep", but I found the snippet of code and used it as it was. As for system(), that was a nasty can of worms. All I wanted was to be able to auto capture a key press to continue exicution of code. Seems there is no really reliable way to emulate the system("pause") command. I found several alternatives, and all had thier faults... and forget the debate that rages over this very topic. My other alternative was to define a switch function that did the same thing.
Quote:
item.h
why is iostream included here? move to item.cpp
the include guards should be placed before any code, even #include
extern void Text_Color(int color); <--- what is this doing here?
The extern fuction Text_Color is used in several of the methods for colored text output. I first tried including engine.h in the item.h then the item.cpp but ran into cyclyc header dependancies. Thats when I instituted the header guards. However, no matter how I implimented the header guards the compiler would puke. Must have had 20 people looking at the code on several forums that couldnt figure it out. So in the end, it was just extern that one fuction rather than include the entire engine.h with alot of un-needed functions.
Quote:
Item do not have any constructor
Set_Item_Type and Get_Item_Type should be virtual if they are overridable or removed from Weapon.
design: why not move name cost and owned to Item.
Weapon should go into its own file.
Set_Weapon_Attributes should be a constructor.
Set_Item_Type and Get_Item_Type should be virtual or removed.
Armor should be moved to its own file.
When I first did the item classes, they were and still are inherited classes from Item. But, when I used virtual it was giving me hastles for some reason, dont remember what it is now. You touched on my original idea where Item would hold the name, cost, item type and other common methods to both the weapon and armor classes... but I didnt have a handle on inheritance when I started and trying to over come that lack of understanding with the other problems I was facing made me drop back and punt on that particular piece of the puzzle. As it stands, Item class is a relic and should be taken out. The reason set_weapon_attributes isnt in the constructor is due to the fact that originally everything was done by value and so the constructor was called alot to copy objects, so it would loose its values when they were set by the constructor. Later when I moved to using reference pointers that would have made more sense.
Quote:
rpg_main.cpp can be made much shorter with something like this:
Weapon *weaponArray[15];
weaponArray[0]=new Weapon(1,"Unarmed Strike",0,1,1,3,20,2);
weaponArray[1]=new Weapon(1,"Brass Knuckles",10,0,1,4,20,2);
.
.
.
for (int i=0; i< numberOfWeapons; i++) {
delete weaponArray;
}
and break main into many small functions.
----edit----
for (int i=0; i< numberOfWeapons; i++) --- I understand what you are doing here, but I dont understand the i<. Can you have a for(;;;;) statement and what does the extra ; mean?----- scratch that. The < came out as "&"lt";" when I was replying.. so it didnt make sense. I guess that is how the < is placed in the back end of the editor here.
I definitely understand braking up main. I got very prociedural toward the end. Im currently revising the code to make it cleaner and will definitely take all this into practice.
Thank you very much for the critique
A for with four ;'s is a broken for. It won't compile. (I've made the mistake of adding a ; after the i++ in my for loops. Ugh)
i< numberOfWeapons; means while "i is less than numberOfWeapons".
i< numberOfWeapons; means while "i is less than numberOfWeapons".
Quote:
Original post by westond Quote:
Original post by me_minus
westond:
I have given your code a little of my time.
One thing that you rely need to do is to use autoformatting or be much more carefull with you indentation.
Visual studio can reformat a complete file.
Thank you so very much. I need all the criticizm I can get. I tried to clean up the indentation as much as possible, VS seemed to chew up most of the formatting. It may just be how I write my code though.
Let VS decide how to indent. when you add a closing brace '}' it should
indent the code. I'm sure you can change the way it does that but I dont know where.
Quote:
Quote:
If you dont need to include something in a header file please dont. (windows.h in engine.h)
Include it in the .cpp instead. Doing this can reduce compile times alot for large projects.
Is this also the case with user made .h files. If I have a class B in a header and cpp named B but class A in header/cpp file named A that referenced B in some way, then I would have to include the B.h file in the A.h file wouldnt I?
I'll reply to this in another comment
Quote:
Quote:
sleep is a very bad funtion name. I don't think it defined by the c library but it is in the posix standard.
system("pause") is not very portable, so please move it to its own portability layer together with the color code
I wondered about "sleep", but I found the snippet of code and used it as it was. As for system(), that was a nasty can of worms. All I wanted was to be able to auto capture a key press to continue exicution of code. Seems there is no really reliable way to emulate the system("pause") command. I found several alternatives, and all had thier faults... and forget the debate that rages over this very topic. My other alternative was to define a switch function that did the same thing.
:-) welcome to the world of portability.
Most people doing portable stuff ends up with a layer that hides the dirty detail. This is one of them. another is '\' vs '/' in filenames.
Quote:
Quote:
item.h
why is iostream included here? move to item.cpp
the include guards should be placed before any code, even #include
extern void Text_Color(int color); <--- what is this doing here?
The extern fuction Text_Color is used in several of the methods for colored text output. I first tried including engine.h in the item.h then the item.cpp but ran into cyclyc header dependancies. Thats when I instituted the header guards. However, no matter how I implimented the header guards the compiler would puke. Must have had 20 people looking at the code on several forums that couldnt figure it out. So in the end, it was just extern that one fuction rather than include the entire engine.h with alot of un-needed functions.
:-) circles in the dependeny graph is bad. but this one is easily broken.
Text_Color should be declared in platform_dependent.h
Text_Color should be implemented in platform_dependent.cpp
plattform_dependent.h should be included in engine.cpp, Weapon.cpp, ...
engine.h should not need any knowledge of Text_Color.
Quote:
Quote:
Item do not have any constructor
Set_Item_Type and Get_Item_Type should be virtual if they are overridable or removed from Weapon.
design: why not move name cost and owned to Item.
Weapon should go into its own file.
Set_Weapon_Attributes should be a constructor.
Set_Item_Type and Get_Item_Type should be virtual or removed.
Armor should be moved to its own file.
When I first did the item classes, they were and still are inherited classes from Item. But, when I used virtual it was giving me hastles for some reason, dont remember what it is now. You touched on my original idea where Item would hold the name, cost, item type and other common methods to both the weapon and armor classes... but I didnt have a handle on inheritance when I started and trying to over come that lack of understanding with the other problems I was facing made me drop back and punt on that particular piece of the puzzle. As it stands, Item class is a relic and should be taken out. The reason set_weapon_attributes isnt in the constructor is due to the fact that originally everything was done by value and so the constructor was called alot to copy objects, so it would loose its values when they were set by the constructor. Later when I moved to using reference pointers that would have made more sense.
can you explain what trouble you got from virtual? there are some thing that SHOULD NOT be done when using virtual (experts might side step the rules, but they know the price they are paying)
ok, so you just need to restructure the code again.
Passing classes by value is usualy a bad idea, which you seem to have realised.
A const reference is much better and if that don't work use just a reference.
Quote:
Quote:
rpg_main.cpp can be made much shorter with something like this:
Weapon *weaponArray[15];
weaponArray[0]=new Weapon(1,"Unarmed Strike",0,1,1,3,20,2);
weaponArray[1]=new Weapon(1,"Brass Knuckles",10,0,1,4,20,2);
.
.
.
for (int i=0; i< numberOfWeapons; i++) {
delete weaponArray;
}
and break main into many small functions.
----edit----
for (int i=0; i< numberOfWeapons; i++) --- I understand what you are doing here, but I dont understand the i<. Can you have a for(;;;;) statement and what does the extra ; mean?----- scratch that. The < came out as "&"lt";" when I was replying.. so it didnt make sense. I guess that is how the < is placed in the back end of the editor here.
My fault! It should have been:
for (int i=0; i<numberOfWeapons; i++)
< is html speak for '<' (less than operator)
Quote:
I definitely understand braking up main. I got very prociedural toward the end. Im currently revising the code to make it cleaner and will definitely take all this into practice.
Thank you very much for the critique
post when you are ready and i'll look over it again, being more of a sadist ;-P
Quote:
Quote:
If you dont need to include something in a header file please dont. (windows.h in engine.h)
Include it in the .cpp instead. Doing this can reduce compile times alot for large projects.
Is this also the case with user made .h files. If I have a class B in a header and cpp named B but class A in header/cpp file named A that referenced B in some way, then I would have to include the B.h file in the A.h file wouldnt I?
This is a rather advanced thing!
A.h:
class A {};
B.h:
class A; // forward declaration of class Aclass B { void insert(const A& a); void insert(const A* const a);};
B.cpp:
#include "b.h"#include "a.h"void B::insert(const A& a) {;}void B::insert(const A* const a) {;}
c.h:
#include "A.h"class C { void insert(const A a);};
c.cpp
#include "c.h"#include "a.h" // the compiler will let you remove this line // but I think it is a bad idea since it is a sort of // documentation of what you are usingvoid C::insert(const A a) {;}
if you look at b.h you will see that there is no include statement. In the case of a class that is only referenced or used as a pointer the compile know the size of data to be copied around but you need to do a forward declaration soo the compiler know of the type (class).
In c.h A is used by value which means that the compile must know the size of A to be able to copy it around. To get the size it must have the full definition of the class.
this can also be used in cpp files as long as you don't use the reference/pointer in that function. You may send it to a method or
function that has knowledge of the full implementation.
Quote:
Original post by me_minus
I got problems with:
friend static std::ostream& operator<<(std::ostream& where, MenuOption& what);
And indeed I don't use it anywhere else, no idea why I wanted it that way >< Thanks for spotting that.
Quote:
Original post by me_minus
some of the constructors have this problem:
class foo {
foo(): two(2),one(1) {;}
int one,two;
};
that gives a warning with gcc to remove the warning use this:
class foo {
foo(): one(1),two(2) {;} <--- changed line
int one,two;
};
I guess it's the same with the parent constructors, they should come first and in the same order as declared? I'm using VC++ 2005 EE, so I don't get warnings about that.
By the way, isn't there a way to suppress specific warnings? I spent quite some time typing in size_type to int castings to clear the build log of warnings about implicit casting, it would be nice to avoid that.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement