C++ doesn't add compare's definition to your declaration, because you didn't add a class name to tell it which header file contains the compare method. That's what "Doctor::" does. It tells the compiler that your compare method's definition defines the method declared in Doctor.
Let's go over your methods for the Doctor class. We'll start with your header file.
The following methods are declared, but not yet defined. The compiler expects to find definitions for all of these in the c++ file with definitions that are exactly the same, except they add Doctor:: before the method name and add a body {} which contains the definition. The compiler needs Doctor:: before the method name to know which class the method belongs to. If there is no class named, the method is a static method that doesn't belong to any class. I will revisit this last part later.
1 2 3 4 5
|
Doctor();
Doctor (std::string n);
Doctor (std::string n, int num);
//virtual ~Doctor();
int compare(Doctor doc2);
|
The following methods are defined. Because they are defined inside a class, the compiler knows these methods are part of the Doctor class. Notice how you were able to declare and define the methods at the same time.
1 2 3 4
|
std::string getName(){return name;}
void setName(std::string);
int getID(){return id;}
void setID(int num);
|
Now for the methods in your c++ file.
Notice how the methods are all outside of a class. This means the unless you specify which class the methods belong to, they will be considered static methods that don't belong to any class. Notice also that the methods have a body {}. This means all methods following are defined. Your constructors tell the compiler that the Doctor() methods belong to the Doctor class because you added Doctor:: before the name. This tells the compiler to look for a declaration in the Doctor class and to add the definition to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
Doctor::Doctor()
{
name = "BLANK NAME";
id = 99999;
}
Doctor::Doctor(string n)
{
name = n;
}
Doctor::Doctor(string n, int fig)
{
name = n;
id = fig;
}
/*Doctor::~Doctor()
{
//dtor
}*/
void Doctor::setName(string n){ name = n;}
void Doctor::setID(int fig){ id = fig;}
|
Now consider what follows. The compiler will see these definitions and see that there is no declaration, because your declarations are in a Class and the method names don't specify a class. This means the compiler sees these as declarations and definitions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
string getName(){ return name;}
int getID() {return id;}
int compare(Doctor doc2){
if (id > doc2.getID()){
return 1;
} else {
if (id < doc2.getID()){
return -1;
} else {
return 0;
}
}
|
This leads me to the final piece. The name and id in your c++ file are static variables. They also do not belong to a class because they are outside of a class. This means that the methods in the last section are using these variables instead of the variables that the Doctor contains. If you delete these, you will get an error for every method that uses those variables and this is a good thing! Now you will see that your static methods were acting on static variables and you can fix the error by adding the class name before the methods.
I suggest you look at
http://www.cplusplus.com/doc/tutorial/classes/
if you have trouble understanding anything I wrote.