Hi all,

I'm in a C++ course right now and I'm having a bit of trouble getting my virtual function to work correctly. Specifically, trouble with the 2 pointers I have for 2 separate menu displays for my Hangman game.

I have 3 *.h and 3 *.cpp files and the file I'm having trouble in is in MainMenu.cpp. The errors say:

Error - error C2440: 'initializing' : cannot convert from 'Menu *' to 'Controller *' 24
Error - error C2440: 'initializing' : cannot convert from 'HangmanMenu *' to 'Controller *' 25

Also, not sure if the code snippet below that shows my MenuLoop() function will be clear enough for you all to help me out or not without pasting snippets from my other files. If this won't help you in diagnosing my logic errors, I will can paste other parts of the file that I feel is pertinent to my "virtual void display(); function.

#include "MainMenu.h"
#include "HangmanMenu.h"
#include "Controller.h"
#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>

using namespace std;

char selection_new;

Menu::Menu()
{
}


void Menu::MenuLoop()
{
	Menu mainMenu;                    //Declare menu type
	HangmanMenu HangMenu;             //Declare HangmanMenu type
	Controller Ctrl;                  //Declare controller type

	Controller *pCon1 = &mainMenu;    
	Controller *pCon2 = &HangMenu;   //Lines 24-25 points to specific classes and their locations  

	pCon1->display();                //Access location of MainMenu display
	pCon2->display();                //Accesses overridden display function to display HangmanMenu

Could you please post your header files for the Controller and Menu class?

Dusktrreader,

I managed to fix the above errors, but I'm getting 2 new errors that point to my Controller.h and Menu.cpp files. I will post these files; if anything else is needed, I can post that too.

Error 1 - error C2011: 'Controller' : 'class' type redefinition 9
Error 2 - error C2084: function 'void Menu::display(void)' already has a body 49

Those two are the remaining errors that I can't figure out. The area that Error 1 is referring to even confuses me more.

Controller.h

#ifndef CONTROLLER_H
#define CONTROLLER _H
#include "LevelFolders.h"
#include <string>

using namespace std;

class Controller
{
	friend ostream& operator << (ostream&, const Controller&);   //Overloads the << operator
	friend istream& operator >> (istream&, const Controller&);   //Overloads the >> operator
	
private:
	string fileName;    //Stores name of file where words are stored
	char in;            //Holds the file input
	string word;        //Holds the word read from the file
	string outFileName; //Holds the name of the output file
	string *pointer;	//Pointer for the word read from the file
	
	
public:

	virtual void display();

	Controller(); // Default Constructor	
	
	string getFileName(); //Accessor method (or getter)

        void setFileName(string name);  //Mutator method (or setter)

	void OpenFile(string fileName);  //Function that opens the file	

	void playGame(string);  //Function allows the user to play hangman 

	void drawHangMan(int guessCount, int TempNumOne, char wordGuessed[]); //Function that draws the man being hung if the player guesses wrong	

};
#endif // CONTROLLER_H

MainMenu.cpp

#include "MainMenu.h"
#include "HangmanMenu.h"
#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>

using namespace std;

char selection_new;

void Menu::display()
{
	Controller::display();
}

Menu::Menu()
{
}


void Menu::MenuLoop()
{
	Menu mainMenu;                    //Declare menu type
	HangmanMenu HangMenu;             //Declare HangmanMenu type
	Controller Ctrl;                  //Declare controller type

	Controller *pCon1 = &mainMenu;    
	Controller *pCon2 = &HangMenu;   //Lines 24-25 points to specific classes and their locations  

	pCon1->display();                //Access location of MainMenu display
	pCon2->display();                //Accesses overridden display function to display HangmanMenu

	while (selection_new != 'X')   //As long as user does not choose QUIT, run the MenuInput loop
	{
       MenuInput();
	}
}

void Menu::MenuInput()
{
	
	display(); 
	SetInput(); //Again, this function would basically consist of cin >> selection;
	DetermineMenuInput();
}

void Menu::display()
{
}

char Menu::SetInput()   //Equates to a cin statement
{
	cin >> selection_new;
	selection_new = toupper(selection_new);
	return selection_new;
}

void Menu::DetermineMenuInput()      //Determine user's menu selection
{
	switch (selection_new)   /*Looks through the selections to see which one matches the user input;
	                       System ("cls") clears the screen before the new data is displayed
	                       then the Menu is re-displayed*/
	{
		case 'A':
			system ("cls");     
			play();			//Allows the user to play a game - not functional yet
			break;
		case 'X':
			system ("cls");   
			cout << endl << endl << endl;
			cout << "                             Thanks for Playing." << endl;
			cout << endl;
			break;	
		default:
			cout << "Invalid selection." << endl;
	}
}

Well, I don't really see what's causing the first error. It seems to be that there is another location where a class called Controller is defined. Everything in the header appears fine. Please post the Menu header file also.

The second error is occurring because you have two display functions defined within the scope of Menu. The first is on line 12 and the second on line 49. Obviously you only need on of these. There is a somewhat finer point to mention here. In the body of the display() function, you are calling Controller::display. Now, since Controller::display is a virtual function, classes that inherit from Controller can implement there own display() functions, and these will be called whenever an instance of the derived class calls its display function. However, if the derived class doesn't need to do anything but call the base class display() function, there is no need to implement a display() function at all. Instead, the display() function of the base class will be called, which is what you are doing anyway.

>> Error 1 - error C2011: 'Controller' : 'class' type redefinition 9
An unfortunate space character has slipped in, preventing the definition of the intended CONTROLLER_H macro ..

Controller.h

#ifndef CONTROLLER_H
// #define CONTROLLER _H        // <--- wrong
#define CONTROLLER_H            // <--- right
#include "LevelFolders.h"
...
commented: Nice catch +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.