Menus Using Virtual Functions

Menus Using Virtual Functions

What is giving me a redefinition of CMenuItems class error in this code?

//cmenu.hclass 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('07',stdout); // "07" 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("
");      for (i=0; ititle");      printf("
Enter a selection: ");      scanf("%d", &sel);      if (sel>0 && sel<=num_commands)         commands[sel]->Do_Command();   } while (sel <= num_commands);}

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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as