Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why am I getting the error: undefined reference to \'AppleFarmer::~AppleFarmer()

ID: 3768103 • Letter: W

Question

Why am I getting the error: undefined reference to 'AppleFarmer::~AppleFarmer()'

This is my code:

Main.cpp

#include<iostream>
#include "AppleFarmer.h"

using namespace std;

int main()
{
int harvest;
int sales;
AppleFarmer applefarmer(0);
//The 30 days of data will be stored in the array at indices 0 ... 29.
while(applefarmer.endOfMonth()==false)
{
//Ask the user for a harvest amount, or read from another array
cout<<"Enter a harvest amount: ";
cin>>harvest;

//Call harvestApples to update the harvest and inventory variables.
applefarmer.harvestApples(harvest);
//Ask the user for a sales amount
cout<<"Enter a sales count: ";
cin>>sales;
applefarmer.sellApples(sales);
//Call sellApples to update the sales and
//inventory variables Print the current inventory
applefarmer.updateCurrentDay();

}

cout<<"Apple inventory: "<<endl;
//Update the current day
//After the harvest and sales arrays are full,
applefarmer.printHarvest();
//calculate the average harvest, and print the value.
cout<<"Average harvest: "<<applefarmer.calculateAverageHarvest()<<endl;
//Calculate the average sales, and print the value.
cout<<"Average sales: "<<applefarmer.calculateAverageSales()<<endl;
return 0;
}

AppleFarmer.h

#ifndef APPLEFARMER_H
#define APPLEFARMER_H

class AppleFarmer
{
public:
AppleFarmer(int);
~AppleFarmer();
void sellApples(int);
void harvestApples(int);
bool endOfMonth();
void updateCurrentDay();
int getInventory();
double calculateAverageHarvest();
double calculateAverageSales();
void printSales();
void printHarvest();
protected:
private:
int sales[30];
int harvest[30];
int maxDays = 30;
int currentDay = 0;
int inventory = 0;
};

#endif // APPLEFARMER_H

AppleFarmer.cpp

#include<iostream>
#include<iomanip>
#include "AppleFarmer.h"

using namespace std;

//Constructor that sets the harvest and sales to zero
AppleFarmer::AppleFarmer(int initVal)
{
//set maxDays to 30
maxDays = 30;
currentDay = 0;
inventory = 0;

for(int i = 0; i < maxDays; i++)
{
sales[i] = initVal;
harvest[i] = initVal;
}
}
//sellApples takes demand apples
//and sets sales of apples for currentDay
//and if demand is less than inventory
//otherwise set sales to zero
void AppleFarmer::sellApples(int demand)
{
if(demand <= inventory)
{
sales[currentDay] = demand;
inventory = inventory - demand;
}
else
{
sales[currentDay] = 0;
}
}
//harvestApples that add the dayHarvest to inventory
void AppleFarmer::harvestApples(int dayHarvest)
{
harvest[currentDay]= dayHarvest;
inventory = inventory + dayHarvest;
}
//Returns true for endoofMonth or returns false
bool AppleFarmer::endOfMonth()
{
//check currentDay is maxDays
if (currentDay == maxDays)
{
return true;
}
else
{
return false;
}
}
void AppleFarmer::updateCurrentDay()
{
currentDay = currentDay + 1;
}

//Calculate average harvest
double AppleFarmer::calculateAverageHarvest()
{
double avgHarvest=0;
for(int x = 0; x < maxDays; x++)
{
avgHarvest+=harvest[x];
}
return avgHarvest/maxDays;
}

//Calculate average sales
double AppleFarmer::calculateAverageSales()
{
double avgSales=0;
for(int x = 0; x < maxDays; x++)
{
avgSales+=sales[x];
}
return avgSales/maxDays;
}

//Return inventory total
int AppleFarmer::getInventory()
{
return inventory;
}

//print harvest sales
void AppleFarmer::printHarvest()
{
int x;
//print day, harvest and sales values for maxDays
cout<<left<<setw(10)<<"Day: "<<setw(10)<<"Harvest"<<setw(10)<<"Sales"<<endl;
for(x = 0; x < maxDays; x++)
cout<<left<<setw(10)<<(x+1)<<setw(10)<<harvest[x]<<setw(10)<<sales[x]<<endl;


}

Explanation / Answer

the reason for the error is

You're declaring a destructor;

~AppleFarmer();.but not defining a body for it.. The declaration is given by you but the function defination of the destructor is missing.

To correct this use

~AppleFarmer();

{ " function body"

}

here we can use a statment like destroy objects within the destructor function to display it.

if the destruction is defined outside the class we will use this:

{

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote