dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: Beginner
Feb 22, 2000

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Menus Using Virtual Functions

Question:
What is giving me a redefinition of CMenuItems class error in this code?
//cmenu.h
class CMenuItem {   
public:
      char title[81];
      virtual void Do_Command(void)=0;
};
//*********************
//CMDS.CPP Defines and initializes menu commands.
#include 
#include 
#include "cmenu.h"

class CMenuBell:public CMenuItem{
   void Do_Command(void);
};
class CMenuSaying:public CMenuItem{
   void Do_Command(void);
};
class CMenuAdd:public CMenuItem{
   void Do_Command(void);
};
void CMenuBell::Do_Command(void){
   putc('\007',stdout); // "\007" rings a bell
}
void CMenuSaying::Do_Command(void){
   puts("If you know the meaning of the universe,");
   puts("Make the sound of one hand clapping.");
}
void CMenuAdd::Do_Command(void)
{
   double x,y;
   printf("Enter a number: ");
   scanf("%lf",&x);
   printf("Enter a number: ");
   scanf("%lf",&y);
   printf("The total is %f.", x+y);
}
int num_commands;
CMenuItem *commands[20];
void Init_Commands(void) 
{
   commands[0] = new CMenuBell;
   strcpy(commands[0]->title, "Sound a bell.");
   commands[1] = new CMenuSaying;
   strcpy(commands[1]->title, "Print a message.");
   commands[2] = new CMenuAdd;
   strcpy(commands[2]->title, "Add two numbers.");
   num_commands=3;
}
//************************
//Main.cpp
#include 
#include "cmenu.h"
#include "CMDS.cpp"
extern int num_commands;
extern CMenuItem *commands[];
void main(void) {
   int i, sel;
   Init_Commands();
   do {
      puts("\nMENU:\n");
      for (i=0; ititle");
      printf("\nEnter a selection: ");
      scanf("%d", &sel);
      if (sel>0 && sel<=num_commands)
         commands[sel]->Do_Command();
   } while (sel <= num_commands);

}

Answer:
The problem is that you're #including the header "cmenu.h" twice:

 //CMDS.CPP Defines and initializes menu commands.
 #include 
 #include 
 #include "cmenu.h" // first time  

 //Main.cpp
 #include 
 #include "cmenu.h" // second time
 #include "CMDS.cpp"
To avoid this, you should add #include guards in "cmenu.h". This will prevent the multiple definition problem:
 #ifndef CMENU_H
 #define CMENU_H
 //cmenu.h
 class CMenuItem {   
 public:
   char title[81];
   virtual void Do_Command(void)=0;
 };
 #endif
It's always a good idea to add #include guards to every header file you write.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date