Thread: Organisiing code

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

    Organisiing code

    Currently I have two files a header file and the main cpp file, I do however want to split these into seperate files aswell. I'm slightly confused cos i've never done this before, what happens with the prototypes of the functions???

    Where do I put them????

    Someone explain!!!!!!!!!!!
    PuterPaul.co.uk - Portfolio site

  2. #2
    Unregistered
    Guest
    header files are usually specific for a given task. That task may be an entire program, a single function that you know you will want to use in other programs, a group of related functions (like mathematical formulae), a class interface, whatever. The function prototypes and class declarations go in the header file. Function definitions go in the cpp file (implementation file). The cpp file must have the same name as the header file, using the different extension. Inline functions are sometimes listed in the header file, and sometimes not.
    Code:
    //mainProgram .h exists but you probably don't see it
    //mainProgram.cpp follows
    #include <iostream.h>
    void display()
    {
       cout << "hello world" << endl;
    }
    int main()
    {
      dipslay();
      return 0;
    }
    
    To break up the above program into reuseable code create two files as follows:
    //insipid.h
    #ifndef INSIPID_H
    #define INSIPID_H
    void display();
    #end
    
    //insipid.cpp
    #include "insipid.h"
    void display()
    {
       cout << "hello world" << endl;
    }
    
    
    Now to reuse those files in a second program
    //program2.h won't be seen usually, but it exists
    //program2.cpp
    #include <iostream.h>
    #include "insipid.h"
    int main()
    {
      display();
      return 0;
    }
    Note that in program2 you don't mention insipid.cpp. It will get linked to program2 automatically by the linker program as program2 is built.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    header files are easy to split up. just make sure you don't redefine stuff by using #ifndef #else #endif statements.

    C++ object files are a bit harder. divide it into functions. include the header files into both files. use prototypes if your function is on the other c++ file. (i think you can do that in a header file, too.) when both compile correctly separately, link them together. if nothing goes wrong, you're done. rinse and repeat.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Ok i'll show u my problem, i have two cpp files and two header files.

    definitions.h :

    Code:
    #ifndef DEFINITIONS_H
    #define DEFINITIONS_H
    #define OK		1
    #define FAILED  0
    
    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    /*Prototypes*/
    void moveAtoB(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    void moveAtoC(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    void moveBtoA(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    void moveBtoC(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    void moveCtoA(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    void moveCtoB(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    
    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;
    		}
    	}
    	int getTop(){ return pegStack.back(); }
    	int stackSize(){ return pegStack.size(); }
    	void removeRing(){ pegStack.pop_back(); }
    	void showStack(){
    		int i;
    		for(i = pegStack.size() - 1; i >= 0; i--)
    		    printf("%5d\n",pegStack[i]);
    	}
    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;
    	}
    	string getDifficulty(){ return setting; }
    private:
    	string setting;
    	int numRings;
    };
    
    struct gameStruct{
    	int noMoves;
    	int minMoves;
    	CPeg* A;
    	CPeg* B;
    	CPeg* C;
    	CDifficulty* diff;
    };
    
    #endif
    interface.h :

    Code:
    #ifndef INTERFACE_H
    #define INTERFACE_H
    
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    
    COption* mainMenu();
    COption* diffMenu();
    COption* playMenu();
    void createRings(struct gameStruct hanoiGame);
    void showPegs(struct gameStruct hanoiGame);
    void mainSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    void diffSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    void playSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame);
    void clearPegs(struct gameStruct hanoiGame);
    
    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)
    	{}
    	string getName(){ return name;}
    	int getNumber(){ return number; }
    private:
    	int number;
    	string name;
    };
    
    class CMenu{
    public:
    	CMenu(const char* itsName = ""):
    	  mName(itsName),noOptions(0)
    	{}
    	CMenu(const string& itsName,COption* optList,const int numOptions):
    	  mName(itsName),o_array(optList),noOptions(numOptions)
    	{}
    	void showMenu(){
    		cout << "#### " << mName << " ####" << endl;
    		for(int i = 0; i<noOptions; i++)
    			cout << o_array[i].getNumber() << ". " 
    					<< o_array[i].getName() << endl;
    		cout << "\nPlease choose an option: ";
    	}
    private:
    	string mName;
    	int noOptions;
    	COption* o_array;
    };
    
    struct menuStruct{
    	CMenu* mainMenu;
    	CMenu* diffMenu;
    	CMenu* playMenu;
    };
    
    #endif
    hanoi.cpp :

    Code:
    #include "definitions.h"
    #include "interface.h"
    
    int main(){
    	vector<int> pStack;
    	menuStruct hanoiMenus;
    	gameStruct hanoiGame;
    
    	/*Create the pegs*/
    	hanoiGame.A = new CPeg(pStack,"A");
    	hanoiGame.B = new CPeg(pStack,"B");
    	hanoiGame.C = new CPeg(pStack,"C");
    
    	/*Set up the move count*/
    	hanoiGame.noMoves = 0;
    	hanoiGame.minMoves = 7;	
    
    	/*Create the difficulty*/
    	hanoiGame.diff = new CDifficulty("easy",3);
    
    	/*Create menu options*/
    	COption* mainOpts = mainMenu();
    	COption* diffOpts = diffMenu();
    	COption* playOpts = playMenu();
    
    	/*Create Menus*/
    	hanoiMenus.mainMenu = new CMenu("MAIN MENU",mainOpts,3);
    	hanoiMenus.diffMenu = new CMenu("DIFFICULTY MENU",diffOpts,4);
    	hanoiMenus.playMenu = new CMenu("PLAY MENU",playOpts,7);
    
    	hanoiMenus.mainMenu->showMenu();
    	mainSelect(hanoiMenus,hanoiGame);
    
    	delete[] mainOpts;
    	delete[] diffOpts;
    	delete[] playOpts;
    	return 0;
    }
    
    void moveAtoB(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int temp;
    
    	temp = hanoiGame.A->getTop();
    
    	if(hanoiGame.B->stackSize() == 0){
    		hanoiGame.A->removeRing();
    		hanoiGame.noMoves++;
    	}
    	else if(temp < hanoiGame.B->getTop()){
    		hanoiGame.A->removeRing();
    		hanoiGame.noMoves++;
    	}
    	hanoiGame.B->addRing(temp);
    
    	system("CLS");
    	showPegs(hanoiGame);
    	hanoiMenus.playMenu->showMenu();
    	playSelect(hanoiMenus,hanoiGame);
    }
    
    void moveAtoC(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int temp;
    
    	temp = hanoiGame.A->getTop();
    
    	if(hanoiGame.C->stackSize() == 0){
    		hanoiGame.A->removeRing();
    		hanoiGame.noMoves++;
    	}
    	else if(temp < hanoiGame.C->getTop()){
    		hanoiGame.A->removeRing();
    		hanoiGame.noMoves++;
    	}
    	hanoiGame.C->addRing(temp);
    
    	system("CLS");
    	showPegs(hanoiGame);
    	hanoiMenus.playMenu->showMenu();
    	playSelect(hanoiMenus,hanoiGame);
    }
    
    void moveBtoA(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int temp;
    
    	temp = hanoiGame.B->getTop();
    
    	if(hanoiGame.A->stackSize() == 0){
    		hanoiGame.B->removeRing();
    		hanoiGame.noMoves++;
    	}
    	else if(temp < hanoiGame.A->getTop()){
    		hanoiGame.B->removeRing();
    		hanoiGame.noMoves++;
    	}
    	hanoiGame.A->addRing(temp);
    
    	system("CLS");
    	showPegs(hanoiGame);
    	hanoiMenus.playMenu->showMenu();
    	playSelect(hanoiMenus,hanoiGame);
    }
    
    void moveBtoC(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int temp;
    
    	temp = hanoiGame.B->getTop();
    
    	if(hanoiGame.C->stackSize() == 0){
    		hanoiGame.B->removeRing();
    		hanoiGame.noMoves++;
    	}
    	else if(temp < hanoiGame.C->getTop()){
    		hanoiGame.B->removeRing();
    		hanoiGame.noMoves++;
    	}
    	hanoiGame.C->addRing(temp);
    
    	system("CLS");
    	showPegs(hanoiGame);
    	hanoiMenus.playMenu->showMenu();
    	playSelect(hanoiMenus,hanoiGame);
    }
    
    void moveCtoA(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int temp;
    
    	temp = hanoiGame.C->getTop();
    
    	if(hanoiGame.A->stackSize() == 0){
    		hanoiGame.C->removeRing();
    		hanoiGame.noMoves++;
    	}
    	else if(temp < hanoiGame.A->getTop()){
    		hanoiGame.C->removeRing();
    		hanoiGame.noMoves++;
    	}
    	hanoiGame.A->addRing(temp);
    
    	system("CLS");
    	showPegs(hanoiGame);
    	hanoiMenus.playMenu->showMenu();
    	playSelect(hanoiMenus,hanoiGame);
    }
    
    void moveCtoB(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int temp;
    
    	temp = hanoiGame.C->getTop();
    
    	if(hanoiGame.B->stackSize() == 0){
    		hanoiGame.C->removeRing();
    		hanoiGame.noMoves++;
    	}
    	else if(temp < hanoiGame.B->getTop()){
    		hanoiGame.C->removeRing();
    		hanoiGame.noMoves++;
    	}
    	hanoiGame.B->addRing(temp);
    
    	system("CLS");
    	showPegs(hanoiGame);
    	hanoiMenus.playMenu->showMenu();
    	playSelect(hanoiMenus,hanoiGame);
    }
    interface.cpp

    Code:
    #include "interface.h"
    
    COption* mainMenu(){
    	COption* o_array = new COption[3];
    
    	COption num1("Choose Difficulty",1);
    	COption num2("Play game",2);
    	COption num3("Exit",3);
    
    	o_array[0] = num1;
    	o_array[1] = num2;
    	o_array[2] = num3;
    
    	return o_array;
    }
    
    COption* diffMenu(){
    	COption* o_array = new COption[4];
    
    	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[7];
    
    	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;
    }
    
    void createRings(struct gameStruct hanoiGame){
    	int i;
    	if(hanoiGame.diff->getDifficulty() == "easy"){
    		for(i = 3; i>0; i--)
    			hanoiGame.A->addRing(i);
    	}else if(hanoiGame.diff->getDifficulty() == "medium"){
    		for(i = 5; i>0; i--)
    			hanoiGame.A->addRing(i);
    	}else if(hanoiGame.diff->getDifficulty() == "hard"){
    		for(i = 7; i>0; i--)
    			hanoiGame.A->addRing(i);
    	}
    }
    
    void showPegs(struct gameStruct hanoiGame){
    	cout << "PEG A\n";
    	hanoiGame.A->showStack();
    	cout << "\nPEG B\n";
    	hanoiGame.B->showStack();
    	cout << "\nPEG C\n";
    	hanoiGame.C->showStack();
    	cout << "\n\nNumber of moves: " << hanoiGame.noMoves;
    	cout << "\nMinimum no. moves: " << hanoiGame.minMoves;
    	cout << "\n\n";
    }
    
    void mainSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int choice = 0;
    
    	cin >> choice;
    
    	switch(choice){
    	case 1:
    		system("CLS");
    		hanoiMenus.diffMenu->showMenu();
    		diffSelect(hanoiMenus,hanoiGame);
    		break;
    	case 2:
    		system("CLS");
    		createRings(hanoiGame);
    		showPegs(hanoiGame);
    		hanoiMenus.playMenu->showMenu();
    		playSelect(hanoiMenus,hanoiGame);
    		break;
    	case 3:
    		exit(0);
    		break;
    	default:
    		cout << "Invalid";
    		break;
    	}
    
    }
    
    void diffSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int choice = 0;
    
    	cin >> choice;
    	switch(choice){
    	case 1:
    		hanoiGame.minMoves = 7;
    		if((hanoiGame.diff->setDifficulty("easy")) != OK){
    			cout << "WE MAY HAVE A PROBLEM";
    			break;
    		}
    		system("CLS");
    		hanoiMenus.mainMenu->showMenu();
    		mainSelect(hanoiMenus,hanoiGame);
    		break;
    	case 2:
    		hanoiGame.minMoves = 31;
    		if((hanoiGame.diff->setDifficulty("medium")) != OK){
    			cout << "WE MAY HAVE A PROBLEM";
    			break;
    		}
    		system("CLS");
    		hanoiMenus.mainMenu->showMenu();
    		mainSelect(hanoiMenus,hanoiGame);
    		break;
    	case 3:
    		hanoiGame.minMoves = 127;
    		if((hanoiGame.diff->setDifficulty("hard")) != OK){
    			cout << "WE MAY HAVE A PROBLEM";
    			break;
    		}
    		system("CLS");
    		hanoiMenus.mainMenu->showMenu();
    		mainSelect(hanoiMenus,hanoiGame);
    		break;
    	case 4:
    		system("CLS");
    		hanoiMenus.mainMenu->showMenu();
    		mainSelect(hanoiMenus,hanoiGame);
    		break;
    	default:
    		cout << "Invalid";
    		break;
    	}
    
    }
    
    void playSelect(struct menuStruct hanoiMenus,struct gameStruct hanoiGame){
    	int choice = 0;
    
    	cin >> choice;
    	switch(choice){
    	case 1:
    		moveAtoB(hanoiMenus,hanoiGame);
    		break;
    	case 2:
    		moveAtoC(hanoiMenus,hanoiGame);
    		break;
    	case 3:
    		moveBtoA(hanoiMenus,hanoiGame);
    		break;
    	case 4:
    		moveBtoC(hanoiMenus,hanoiGame);
    		break;
    	case 5:
    		moveCtoA(hanoiMenus,hanoiGame);
    		break;
    	case 6:
    		moveCtoB(hanoiMenus,hanoiGame);
    		break;
    	case 7:
    		system("CLS");
    		clearPegs(hanoiGame);
    		hanoiGame.noMoves = 0;
    		hanoiMenus.mainMenu->showMenu();
    		mainSelect(hanoiMenus,hanoiGame);
    		break;
    	default:
    		cout << "Invalid";
    		break;
    	}
    }
    
    void clearPegs(struct gameStruct hanoiGame){
    	int temp;
    	int i;
    
    	/*Clear A*/
    	temp = hanoiGame.A->stackSize();
    	if(temp > 0){
    		for(i = temp; i>0; i--)
    			hanoiGame.A->removeRing();
    	}
    
    	/*Clear B*/
    	temp = hanoiGame.B->stackSize();
    	if(temp > 0){
    		for(i = temp; i>0; i--)
    			hanoiGame.B->removeRing();
    	}
    
    	/*Clear C*/
    	temp = hanoiGame.C->stackSize();
    	if(temp > 0){
    		for(i = temp; i>0; i--)
    			hanoiGame.C->removeRing();
    	}
    
    }
    I get like 127 errors now that i've divided the code up, heres the first few.


    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(8) : error C2143: syntax error : missing ';' before '*'
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(8) : error C2501: 'COption' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(8) : error C2501: 'mainMenu' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(9) : error C2143: syntax error : missing ';' before '*'
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(9) : error C2501: 'COption' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(9) : error C2086: 'COption' : redefinition
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(9) : error C2501: 'diffMenu' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(10) : error C2143: syntax error : missing ';' before '*'
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(10) : error C2501: 'COption' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(10) : error C2086: 'COption' : redefinition
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(10) : error C2501: 'playMenu' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(38) : error C2629: unexpected 'class CMenu ('
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(38) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(51) : error C2143: syntax error : missing ';' before '*'
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(51) : error C2501: 'COption' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\hanoi\interface.h(51) : error C2501: 'o_array' : missing storage-class or type specifiers
    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(22) : error C2065: 'mainOpts' : undeclared identifier
    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(22) : error C2440: '=' : cannot convert from 'int *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(23) : error C2065: 'diffOpts' : undeclared identifier
    C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\MYPROJECTS\hanoi\hanoi.cpp(23) : error C2440: '=' : cannot convert from 'int *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    PuterPaul.co.uk - Portfolio site

  5. #5
    Unregistered
    Guest
    Here's how I would approach your list of errors:

    In C++ the keyword struct is only used in place of the keyword class. You con't use it in function prototypes/definitions. You don't declare menuStruct or gameStruct in the posted code, so I assume they are listed elsewhere. I assume they are C++ structs, at least I wouldn't recommend mixing C and C++ structs in the same program as it is just to confusing.

    any objects of any class not defined in the current h file need to have their header file listed as an include. iostream does that for cin and cout. You need to do that for menuStruct and gameStruct in definitions.h and in interface.h.

    The compiler needs to know the size of an object pointed to by a pointer, even if it is a return value. Thererfore, you need to declare the class before you use pointers to objects of the class in the following lines.

    COption* mainMenu();
    COption* diffMenu();
    COption* playMenu();
    //etc.
    class COption{
    //etc.

    At the very least the compiler needs to know that a class will be declared later by listing the class, even if it doesn't have the whole class yet, before declaring an object of the class.

    When tracking down errors, attack the first one listed first. It may resolve all the others, or create a new list for you to work with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM