Advertisement

C++ Workshop - Project 1

Started by August 16, 2006 05:41 PM
193 comments, last by me_minus 15 years, 3 months ago
No, it's telling you to #define it before you even include windows.h. That way, it sees a definition of _WIN32_WINNT (or WINVER) already, so it doens't re-define. System headers are always (hopefully!) implemented in such a way that the end user never needs to modify them.
popcorn:

Be consistent with #include and file names (upper/lower case)
For the include guard to work it must have the same name in both #ifndef and #define

iostream should almost never be included into a .h file.

You can place a struct or class inside a class. This is good since you can control the visibility.

dont do:
class Menu {  public:    Menu::Menu();}

The Menu:: part is bad.

don't use #define to create constants and enums. (Game.h)


I think this would be clearer (untested):
void Menu::addOptions(vector<Option> optionItem){        copy(optionItem.begin(),optionItem.end(),back_inserter(options));}

Advertisement
Thanks for all the feedback me_minus.

Quote:
Original post by me_minus
popcorn:

Be consistent with #include and file names (upper/lower case)
For the include guard to work it must have the same name in both #ifndef and #define



I knew I had done something stupid but yeah it works now without the pragma's.

Quote:
Original post by me_minus
You can place a struct or class inside a class. This is good since you can control the visibility.

You couldn't give me any examples of this could you or links to websites/pages with info on this subject?

Quote:
Original post by me_minus
don't use #define to create constants and enums. (Game.h)


Fair point, I am just used to using #define for constants now but I do know you can use const int etc which would be a better way of doing things. I will try to do this in future.

Quote:
Original post by me_minus
I think this would be clearer (untested):
void Menu::addOptions(vector<Option> optionItem){        copy(optionItem.begin(),optionItem.end(),back_inserter(options));}



Just tested this quickly and it compiles and works fine - doesn't seem to cause any problems so yeah it is a lot clearer.
How about them apples?
This is not the best example...
I hope it works, this computer/network do not have any compilers on it.

The MenuItem implementaion is hidden from the user.

If you change the first private: to public: it is possible to create
MenuItem's by doing this:
  Menu::MenuItem item('a',"wakeup","Ring the bell of wakeup.");  std::cout << item << std::endl;


The example:
#include <iostream>#include <map>#include <string>class Menu{ private:  class MenuItem   {   public:    MenuItem(const char shortcut, const std::string& text, const std::string& explanation): shortcut_(shortcut), text_(text), explanation_(explanation) {;}   private:      char shortcut_;     std::string text_;     std::string explanation_;     friend std::ostream& operator<<(std::ostream& s,const MenuItem& item);  }; public:  Menu();  void add(int number,const std::string& text,const std::string& explantaion)  { items.insert(make_pair(number,MenuItem(text[0],text,explanation))); }  void draw(std::ostream& s)  {     for (iter=items.begin(); iter!=items.end(); ++iter) {       s << (*iter).first << ": " << (*iter).second << endl;    }  private:  std::map<int,MenuItem> items;  };std::ostream& operator<<(std::ostream& s,const MenuItem& item){  // could be extended by marking the shortcut letter in the text  s << item.text_ << " : " << item.explanation_;   return s;}
me_minus I can see from your code that it is possible to declare a class within a class but what I don't understand is how can menu access MenuItem's private data without accessor functions. Also don't you have to declare the type of iterator you want before using it inside the for loops draw member function.
How about them apples?
You are right about the iterator stuff, I forgot to fix it.

Why should Menu need to access MenuItem's internal state?

operator<< is overloaded to print a MenuItem in 'global' scope.
Even if it is in 'global' scope it can access MenuItems internals
by beeing friend to it.
Advertisement
Ok I've had another look at the code now and I think I understand how it works. I can see why you have no need for accessor functions now. The add member function in Menu creates the the new item using the MenuItem class constructor.
From what I understand everything looks correct, however when I put the code into Visual C++ 2005 it came up with some compiler errors. Here is the code:

#include <iostream>#include <map>#include <string>class Menu{private:	class MenuItem 	{	public:		MenuItem(const char shortcut, const std::string &text, const std::string &explanation): shortcut_(shortcut), text_(text), explanation_(explanation) {}	private: 		char shortcut_;		std::string text_;		std::string explanation_;		friend std::ostream& operator<<(std::ostream &s, const MenuItem &item);	};public:	Menu();	void add(int number, const std::string &text, const std::string &explanation)	{ 		items.insert(std::make_pair(number, MenuItem(text[0], text, explanation))); 	}	void draw(std::ostream &s)	{ 		std::map<int, MenuItem>::const_iterator iter;		for(iter = items.begin(); iter != items.end(); ++iter)		{ 			s << (*iter).first << ": " << (*iter).second << std::endl;		} 	} private:  std::map<int, MenuItem> items;  };std::ostream& operator<<(std::ostream &s, const MenuItem &item){  // could be extended by marking the shortcut letter in the text  s << item.text_ << " : " << item.explanation_;   return s;}


I've reformatted it for my own preferences. The problem seems to be caused by the overloaded operator friend function, however I don't understand why - It looks correct to me.

The errors I get are:
--
Compiling...
test.cpp
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2143: syntax error : missing ',' before '&'

error C2065: 'item' : undeclared identifier

error C2228: left of '.text_' must have class/struct/union
type is ''unknown-type''

error C2228: left of '.explanation_' must have class/struct/union
type is ''unknown-type''

test - 5 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any ideas on why this happens?

I tried the following and it works
class MenuItem 	{	public:		MenuItem(const char shortcut, const std::string &text, const std::string &explanation): shortcut_(shortcut), text_(text), explanation_(explanation) {}	private: 		char shortcut_;		std::string text_;		std::string explanation_;		friend std::ostream& operator<<(std::ostream &s, const MenuItem &item)		{			// could be extended by marking the shortcut letter in the text			s << item.text_ << " : " << item.explanation_; 			return s;		}	};


But I don't understand why this works and the other way doesn't.

[Edited by - popcorn on October 5, 2006 7:22:00 PM]
How about them apples?
std::ostream& operator<<(std::ostream &s, const MenuItem &item)
is the faulty line. Change it to
std::ostream& operator<<(std::ostream &s, const Menu::MenuItem &item)
and it might work.

I dont know if there will be trouble with MenuItem is private.
Quote:
Original post by me_minus
std::ostream& operator<<(std::ostream &s, const MenuItem &item)
is the faulty line. Change it to
std::ostream& operator<<(std::ostream &s, const Menu::MenuItem &item)
and it might work.

I dont know if there will be trouble with MenuItem is private.


Sorry it doesn't, you get the following error:

error C2248: 'Menu::MenuItem' : cannot access private class declared in class 'Menu'

It works if you change private to a public though I'm not sure if this would be considered a good thing or not.

oops..reread one of your earlier posts and thats what you said I should do the first time round. I think I understand what's going on now. Thanks for all your help.

[Edited by - popcorn on October 6, 2006 8:42:43 PM]
How about them apples?
I'm hoping that someone is still watching this thread, as I have stumbled across this just last week. I have completed, to some degree, this project and would like some opinions and criticisms. So, if anyone is reading this thread, please take a look and let me know what you think. Hopefully I will begin Project 2 shortly.

Download: Arena.zip
-akusei

This topic is closed to new replies.

Advertisement