Thread: Guidance needed!!

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    87

    Guidance needed!!

    I've defined two classes, one called CMenu (represents a menu) and one called COption (represents options in a menu). Now in my menu constructor I'm wanting to setup an array of options for the menu to have. Here are the two classes.

    Code:
    class CMenu{
    public:
    	CMenu(const char* itsName = ""):
    	  mName(itsName),noOptions(numOptions)
    	{}
    	CMenu(const string& itsName,COption* optList[]):
    	  mName(itsName),o_array(optList)
    	{}
    private:
    	string mName;
    	COption* o_array[];
    };
    
    class COption{
    public:
    	COption(const char* itsName = "",const int optNumber = 0):
    	  name(itsName),number(optNumber)
    	{}
    	COption(const string& itsName,const int optNumber):
    	  name(itsName),number(optNumber)
    	{}
    private:
    	int number;
    	string name;
    };
    In my main source file I have tried to set up a menu with options as follows.

    Code:
    /*Prototypes*/
    COption* mainMenu();
    
    int main(){
    	vector<int> pStack;
    	CPeg* A = new CPeg(pStack,"A");
    	CPeg* B = new CPeg(pStack,"B");
    	CPeg* C = new CPeg(pStack,"C");
    	COption* mainOpts = mainMenu();
    	CMenu* mainMenu = new CMenu("MAIN MENU",mainOpts);
    
    	delete A,B,C;
    	delete[] mainOpts;
    	return 0;
    }
    
    COption* mainMenu(){
    	COption* o_array = new COption[5];
    
    	COption num1("Choose Difficulty",1);
    	COption num2("Read rules",2);
    	COption num3("Play game",3);
    	COption num4("Leader board",4);
    	COption num5("Exit",5);
    
    	o_array[0] = num1;
    	o_array[1] = num2;
    	o_array[2] = num3;
    	o_array[3] = num4;
    	o_array[4] = num5;
    
    	return o_array;
    }
    One problem, I get a list of the following errors, what do I do to rectify this???


    c:\program files\microsoft visual studio\myprojects\hanoi\definitions.h(74) : error C2629: unexpected 'class CMenu ('
    c:\program files\microsoft visual studio\myprojects\hanoi\definitions.h(74) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
    c:\program files\microsoft visual studio\myprojects\hanoi\definitions.h(79) : error C2143: syntax error : missing ';' before '*'
    c:\program files\microsoft visual studio\myprojects\hanoi\definitions.h(79) : error C2501: 'COption' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hanoi\definitions.h(79) : error C2501: 'o_array' : missing storage-class or type specifiers
    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(12) : error C2661: 'CMenu::CMenu' : no overloaded function takes 2 parameters
    Error executing cl.exe.

    hanoi.exe - 6 error(s), 0 warning(s)
    PuterPaul.co.uk - Portfolio site

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Heres an update

    Code:
    class COption{
    public:
    	COption(const char* itsName = "",const int optNumber = 0):
    	  name(itsName),number(optNumber)
    	{}
    	COption(const string& itsName,const int optNumber):
    	  name(itsName),number(optNumber)
    	{}
    private:
    	int number;
    	string name;
    };
    
    class CMenu{
    public:
    	CMenu(const char* itsName = ""):
    	  mName(itsName)
    	{}
    	CMenu(const string& itsName,COption* optList[]):
    	  mName(itsName),o_array(optList)
    	{}
    private:
    	string mName;
    	COption* o_array[];
    };
    Code:
    /*Prototypes*/
    COption* mainMenu();
    
    int main(){
    	vector<int> pStack;
    	CPeg* A = new CPeg(pStack,"A");
    	CPeg* B = new CPeg(pStack,"B");
    	CPeg* C = new CPeg(pStack,"C");
    	COption* mainOpts = mainMenu();
    	CMenu* mainMenu = new CMenu("MAIN MENU",mainOpts);
    
    	delete A,B,C,mainMenu;
    	delete[] mainOpts;
    	return 0;
    }
    
    COption* mainMenu(){
    	COption* o_array = new COption[5];
    
    	COption num1("Choose Difficulty",1);
    	COption num2("Read rules",2);
    	COption num3("Play game",3);
    	COption num4("Leader board",4);
    	COption num5("Exit",5);
    
    	o_array[0] = num1;
    	o_array[1] = num2;
    	o_array[2] = num3;
    	o_array[3] = num4;
    	o_array[4] = num5;
    
    	return o_array;
    }
    c:\program files\microsoft visual studio\myprojects\hanoi\definitions.h(92) : warning C4200: nonstandard extension used : zero-sized array in struct/union
    c:\program files\microsoft visual studio\myprojects\hanoi\definitions.h(89) : error C2536: 'CMenu::o_array' : cannot specify explicit initializer for arrays
    c:\program files\microsoft visual studio\myprojects\hanoi\definitions.h(92) : see declaration of 'o_array'
    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(12) : error C2664: '__thiscall CMenu::CMenu(const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class COption *[])' : cannot convert para
    meter 2 from 'class COption *' to 'class COption *[]'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.

    hanoi.exe - 2 error(s), 1 warning(s)
    PuterPaul.co.uk - Portfolio site

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Ahha, got rid of those errors, heres an update for anyone that might be interested. In case you haven't guessed i'm making a Towers of Hanoi clone. If you have any comments or suggestions on my progress so far then that would be greatly appreciated.

    Header File:
    Code:
    #ifndef DEFINITIONS_H
    #define DEFINITIONS_H
    #define OK        1
    #define FAILED  0
    
    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    struct ring{
    	int size;
    };
    
    class CPeg{
    public:
    	CPeg(vector<int> pStack,const string& itsName):
    	  pegStack(pStack),name(itsName)
    	{}
    	int addRing(int rSize){
    		if(pegStack.size() > 0){
    			if(rSize < pegStack.back()){
    				pegStack.push_back(rSize);
    				return OK;
    			}else
    				return FAILED;
    		}else{
    			pegStack.push_back(rSize);
    			return OK;
    		}
    	}
    	void removeRing(){ pegStack.pop_back(); }
    	void showStack(){
    		for(int i = pegStack.size() - 1; i >= 0; i--)
    			cout << pegStack[i] << endl;
    	}
    private:
    	vector<int> pegStack;
    	string name;
    };
    
    class CDifficulty{
    public:
    	CDifficulty(const char* itsSetting = "",const int nRings = 3):
    	  setting(itsSetting),numRings(nRings)
    	{}
    	CDifficulty(const string& itsSetting,int nRings):
    	  setting(itsSetting),numRings(nRings)
    	{}
    	int setDifficulty(const string itsSetting){
    		setting = itsSetting;
    		if(setting == "easy"){
    			numRings = 3;
    			return OK;
    		}else if(setting == "medium"){
    			numRings = 5;
    			return OK;
    		}else if(setting == "hard"){
    			numRings = 7;
    			return OK;
    		}else
    			return FAILED;
    	}
    private:
    	string setting;
    	int numRings;
    };
    Main source file:
    Code:
    #include "definitions.h"
    
    /*Prototypes*/
    COption* mainMenu();
    COption* diffMenu();
    COption* playMenu();
    
    int main(){
    	vector<int> pStack;
    
    	/*Create the pegs*/
    	CPeg* A = new CPeg(pStack,"A");
    	CPeg* B = new CPeg(pStack,"B");
    	CPeg* C = new CPeg(pStack,"C");	
    
    	/*Create menu options*/
    	COption* mainOpts = mainMenu();
    	COption* diffOpts = diffMenu();
    	COption* playOpts = playMenu();
    
    	/*Create Menus*/
    	CMenu* mainMenu = new CMenu("MAIN MENU",mainOpts);
    	CMenu* diffMenu = new CMenu("DIFFICULTY MENU",diffOpts);
    	CMenu* playMenu = new CMenu("PLAY MENU",playOpts);
    
    	delete A,B,C,mainMenu;
    	delete diffMenu,playMenu;
    	delete[] mainOpts;
    	delete[] diffOpts;
    	delete[] playOpts;
    	return 0;
    }
    
    COption* mainMenu(){
    	COption* o_array = new COption[4];
    
    	COption num1("Choose Difficulty",1);
    	COption num2("Read rules",2);
    	COption num3("Play game",3);
    	COption num4("Leader board",4);
    	COption num5("Exit",5);
    
    	o_array[0] = num1;
    	o_array[1] = num2;
    	o_array[2] = num3;
    	o_array[3] = num4;
    	o_array[4] = num5;
    
    	return o_array;
    }
    
    COption* diffMenu(){
    	COption* o_array = new COption[3];
    
    	COption num1("Easy",1);
    	COption num2("Medium",2);
    	COption num3("Hard",3);
    	COption num4("Main menu",4);
    
    	o_array[0] = num1;
    	o_array[1] = num2;
    	o_array[2] = num3;
    	o_array[3] = num4;
    
    	return o_array;
    }
    
    COption* playMenu(){
    	COption* o_array = new COption[6];
    
    	COption num1("Move From A to B",1);
    	COption num2("Move From A to C",2);
    	COption num3("Move From B to A",3);
    	COption num4("Move From B to C",4);
    	COption num5("Move From C to A",5);
    	COption num6("Move From C to B",6);
    	COption num7("Main Menu",7);
    
    	o_array[0] = num1;
    	o_array[1] = num2;
    	o_array[2] = num3;
    	o_array[3] = num4;	
    	o_array[4] = num5;	
    	o_array[5] = num6;
    	o_array[6] = num7;
    
    	return o_array;
    }
    PuterPaul.co.uk - Portfolio site

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. Guidance, please.
    By mattagrimonti in forum C Programming
    Replies: 2
    Last Post: 11-26-2008, 08:50 AM
  3. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  4. Guidance Needed...
    By able in forum C Programming
    Replies: 1
    Last Post: 06-28-2006, 11:59 PM
  5. need guidance to connect to serial port
    By gnychis in forum Linux Programming
    Replies: 1
    Last Post: 06-02-2005, 10:10 AM