Build Error, Undefined reference to [function]
Hi, so I have written a program to help me learn virtual methods, and I get an error every time i build it, saying:
1 2
|
<path>\12-10_DataSlicingWhenPassingByValue.o:12-10_DataSlicingWhenPassingByValue.cpp|| undefined reference to `PtrFunction(Mammal*)'|
||=== Build finished: 1 errors, 0 warnings ===|
|
even though I have clearly defined the function and implemented 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
using namespace std;
class Mammal
{
public:
Mammal():itsAge(1) {}
virtual ~Mammal() {} //I suspect destructor needs to be virtual so that the derived class's destructor can be called?
virtual void Speak() const {cout<< "Mammal speak!\n"; }
protected:
int itsAge;
};
class Dog : public Mammal
{
public:
void Speak() const {cout<< "Woof!\n";}
};
class Cat: public Mammal
{
public:
void Speak() const {cout<< "Meow!\n";}
};
void ValueFunction(Mammal);
void PtrFunction(Mammal*);
void RefFunction(Mammal&);
int main()
{
Mammal* ptr = 0;
int choice;
while(1)
{
bool fQuit = false;
cout<< "(1)dog (2)cat (0)Quit: ";
cin >> choice;
switch (choice)
{
case 0: fQuit = true;
break;
case 1: ptr = new Dog;
break;
case 2: ptr = new Cat;
break;
default: ptr = new Mammal;
break;
}
if (fQuit == true)
break;
PtrFunction(ptr);
RefFunction(*ptr);
ValueFunction(*ptr);
}//end while
return 0;
}
void ValueFunction(Mammal MammalValue)
{
MammalValue.Speak();
}
void PrtFunction(Mammal* pMammal)
{
pMammal -> Speak();
}
void RefFunction(Mammal & rMammal)
{
rMammal.Speak();
}
|
Is there something I am doing wrong?
Thanks in advance.
Edit: I have just realized this should probably be in beginner's forum, my apologies
Last edited on
Spot the spelling mistake :-)
*facepalm
thanks!
Topic archived. No new replies allowed.