I have included
#ifndef FileName_H
#define FileName_H
...
#endif
within all of my header files. In my main.cpp
, I would like to use polymorphism of a class Worker
:
Worker * w = NULL;
w = new Employee(001,"Tom",3);
w->showInfo();
delete w;
Worker * w = NULL;
w = new Manager(002,"Bob",1);
w->showInfo();
delete w;
Worker * w = NULL;
w = new Boss(003,"Ann",2);
w->showInfo();
delete w;
However, the error: redefinition of 'w'
always occurs. I have included #ifndef
statements, and I have deleted the pointer and set it NULL
before reusing it. I don't know why this error can still occur. Could you give me some possible reasons or clues?
Exactly what the error message says.
Worker * w = NULL;
w = new Employee(001,"Tom",3);
w->showInfo();
delete w;
Worker * w = NULL; // <<<<<<< here's the problem
w = new Manager(002,"Bob",1);
w->showInfo();
delete w;
The second assignment to the pointer redefines the pointer variable, which isn't allowed within the same scope. Your code should look like this instead:
Worker * w = NULL;
w = new Employee(001,"Tom",3);
w->showInfo();
delete w;
w = new Manager(002,"Bob",1); // No type declaration!
w->showInfo();
delete w;