I am struggling to identify the issue with my code in my class inheritance. I've searched many places but can't quite seem to find the right answer. I've made some progress to fix many errors before this, but haven't wrapped it all up quite yet.
The main issue I am running into is undefined references to either methods or constructors in many instances throughout my files. I'm sure I'm probably just missing something stupidly simple somewhere, but being fairly new to C++, it's difficult for me to pinpoint exactly where the issue lies.
Below are the files to my code:
dog.h
#ifndef DOG_H
#define DOG_H
#include <string>
using namespace std;
class Dog{
public:
Dog();
~Dog(){};
string dogName = "";
string Breed = "";
int age = 0;
int weight = 0;
bool subjectToDiscount = false;
int riskWeight = 0;
float riskPremium = 0;
float basePremium = 0;
virtual float getPremium();
virtual Dog getDog(char b);
protected:
virtual float getBasePremium();
private:
};
#endif //DOG_H
dog.cpp
#include <iostream>
#include <string>
#include "dog.h"
#include "breeds.h"
using namespace std;
//---default constructor---//
Dog::Dog(){
};
//---methods---//
float Dog::getBasePremium(){
float val = 0;
//calculate base premium based on weight
return val;
};
float Dog::getPremium(){
float val = 0;
//calculate actual premium for the dog in question
if(this->riskWeight == 0){
val = this->basePremium;
}else if(this->weight > this-> riskWeight){
val = this->riskPremium;
}else val = this->basePremium;
//add discount if applicable
if(this->subjectToDiscount && this->age > 13) val *= 0.80;
//add 25% if over 50kg
if(this->weight > 50) val *= 1.25;
return val;
};
Dog Dog::getDog(char b){
bool recognized = false;
Dog *pup;
while(!recognized){
switch (b)
{
case 'p':
pup = new Pitbull();
recognized = true;
break;
case 'd':
pup = new Doberman();
recognized = true;
break;
case 'r':
pup = new Rottweiler();
recognized = true;
break;
default:
cout << "Breed code not recognized, please try again...\n";
break;
}
}
return *pup;
}
int main(){
return 0;
}
breeds.h
#ifndef BREEDS_H
#define BREEDS_H
#include "dog.h"
#include <string>
using namespace std;
class Pitbull : public Dog{
public:
Pitbull();
~Pitbull(){};
protected:
private:
};
class Doberman : public Dog{
public:
Doberman();
~Doberman(){};
protected:
private:
};
class Rottweiler : public Dog{
public:
Rottweiler();
~Rottweiler(){};
protected:
private:
};
#endif //BREEDS_H
breeds.cpp
#include <iostream>
#include <string>
#include "dog.h"
#include "breeds.h"
using namespace std;
//---constructors---//
Pitbull::Pitbull(){
this->Breed = "a Pitbull";
this->basePremium = 30.20f;
this->riskPremium = 35.15f;
this->riskWeight = 20;
this->subjectToDiscount = false;
}
Doberman::Doberman(){
this->Breed = "a Doberman";
this->basePremium = 28.16f;
this->riskPremium = 30.00f;
this->riskWeight = 35;
this->subjectToDiscount = true;
}
Rottweiler::Rottweiler(){
this->Breed = "a Rottweiler";
this->basePremium = 28.00f;
this->riskPremium = 29.75f;
this->riskWeight = 45;
this->subjectToDiscount = false;
}
int main(){
return 0;
}
main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "dog.h"
#include "breeds.h"
using namespace std;
int getDogCount(){
int retVal = 0;
cout << "Please enter the number of dogs in your household: ";
cin >> retVal;
return retVal;
}
void run(){
cout << setiosflags(ios::fixed);
cout << setprecision(2);
int dogCount = 0;
float totalPremium = 0;
dogCount = getDogCount();
for(int i = 1; i <= dogCount; i++){
float premium = 0;
char breedCode = '.';
string dogName = "";
Dog pup;
cout << "Enter the name of dog #" << i << ": ";
cin.ignore();
getline(cin, dogName);
cout << "Enter the breed code for " << dogName << ": ";
cin >> breedCode;
cin.ignore();
pup = pup.getDog(breedCode);
pup.dogName = dogName;
cout << "Enter the current age for " << dogName << ": ";
cin >> pup.age;
cin.ignore();
cout << "Enter the current weight for " << dogName << ": ";
cin >> pup.weight;
premium = pup.getPremium();
cout << "\n";
}
}
int main(){
run();
return 0;
}
These are the errors I get after attempting to compile the 3 .cpp files:
dog.cpp
C:\Users\whitl\AppData\Local\Temp\ccFdz6zr.o: In function `Dog::getDog(char)':
C:\Sandbox\C++\Beginning\Wooffurs/dog.cpp:46: undefined reference to `Pitbull::Pitbull()'
C:\Sandbox\C++\Beginning\Wooffurs/dog.cpp:50: undefined reference to `Doberman::Doberman()'
C:\Sandbox\C++\Beginning\Wooffurs/dog.cpp:54: undefined reference to `Rottweiler::Rottweiler()'
collect2.exe: error: ld returned 1 exit status
breeds.cpp
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o: In function `Pitbull::Pitbull()':
C:\Sandbox\C++\Beginning\Wooffurs/breeds.cpp:10: undefined reference to `Dog::Dog()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o: In function `Doberman::Doberman()':
C:\Sandbox\C++\Beginning\Wooffurs/breeds.cpp:18: undefined reference to `Dog::Dog()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o: In function `Rottweiler::Rottweiler()':
C:\Sandbox\C++\Beginning\Wooffurs/breeds.cpp:26: undefined reference to `Dog::Dog()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV10Rottweiler[_ZTV10Rottweiler]+0x10): undefined reference to `Dog::getPremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV10Rottweiler[_ZTV10Rottweiler]+0x18): undefined reference to `Dog::getDog(char)'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV10Rottweiler[_ZTV10Rottweiler]+0x20): undefined reference to `Dog::getBasePremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV8Doberman[_ZTV8Doberman]+0x10): undefined reference to `Dog::getPremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV8Doberman[_ZTV8Doberman]+0x18): undefined reference to `Dog::getDog(char)'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV8Doberman[_ZTV8Doberman]+0x20): undefined reference to `Dog::getBasePremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV7Pitbull[_ZTV7Pitbull]+0x10): undefined reference to `Dog::getPremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV7Pitbull[_ZTV7Pitbull]+0x18): undefined reference to `Dog::getDog(char)'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV7Pitbull[_ZTV7Pitbull]+0x20): undefined reference to `Dog::getBasePremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$.refptr._ZTV3Dog[.refptr._ZTV3Dog]+0x0): undefined reference to `vtable for Dog'
collect2.exe: error: ld returned 1 exit status
main.cpp
C:\Users\whitl\AppData\Local\Temp\ccgKyBTC.o: In function `run()':
C:\Sandbox\C++\Beginning\Wooffurs/main.cpp:30: undefined reference to `Dog::Dog()'
C:\Sandbox\C++\Beginning\Wooffurs/main.cpp:40: undefined reference to `Dog::getDog(char)'
C:\Sandbox\C++\Beginning\Wooffurs/main.cpp:50: undefined reference to `Dog::getPremium()'
C:\Users\whitl\AppData\Local\Temp\ccgKyBTC.o:main.cpp:(.rdata$.refptr._ZTV3Dog[.refptr._ZTV3Dog]+0x0): undefined reference to `vtable for Dog'
collect2.exe: error: ld returned 1 exit status
Thank you to anyone that helps, I really appreciate it. Please give my any pointers on my code as well, and how to possibly better pose questions on Stack Overflow.