Reputation: 69
I got a trouble while compiling the following source code [linker error] undefined reference to 'dish::dish()' [linker error] undefined reference to 'dish::~dish()' [linker error] undefined reference to 'dish::ShowResult()' Can anybody help me?
THE HEADER FILE(dish.h):
#ifndef DISH_H
#define DISH_H
class dish {
public:
dish();
dish(std::string name, std::string variety, float caloric, float price);
~dish();
static int GetN();
void SetN(int N);
static int IncrementN();
std::string GetName() const;
void SetName(std::string name);
std::string GetVariety() const;
void SetVariety(std::string variety);
float GetCaloric() const;
void SetCaloric(float caloric);
float GetPrice() const;
void SetPrice(float price);
void Enter();
void ShowResult();
private:
std::string name;
std::string variety;
float caloric;
float price;
static int N;
};
int dish::N;
#endif
and the dish.cpp:
#include <iostream>
#include <cstring>
#include "dish.h"
dish::dish()
{
dish::Enter();
}
dish::dish(std::string name, std::string variety, float caloric, float price)
{
this->name = name;
this->variety = variety;
this->caloric = caloric;
this->price = price;
}
dish::~dish()
{
}
static int dish::GetN()
{
return N;
}
void dish::SetN(int N)
{
this->N = N;
}
static int dish::IncrementN()
{
N++;
}
std::string dish::GetName() const
{
return name;
}
void dish::SetName(std::string name)
{
dish::name = name;
}
std::string dish::GetVariety() const
{
return variety;
}
void dish::SetVariety(std::string variety)
{
dish::variety = variety;
}
float dish::GetCaloric() const
{
return caloric;
}
void dish::SetCaloric(float caloric)
{
this->caloric = caloric;
}
float dish::GetPrice() const
{
return price;
}
void dish::SetPrice(float price)
{
this->price = price;
}
void dish::Enter()
{
std::cout << "\n \\*_________________________________*\\\n";
std::cout << "\n ENTER THE NAME OF DISH: ";
getline(std::cin, name);
std::cout << " ENTER THE VARIETY: ";
getline(std::cin, variety);
std::cout << " ENTER THE CALORIC CONTENT: ";
(std::cin >> caloric).get();
std::cout << " ENTER THE PRICE: ";
(std::cin >> price).get();
std::cout << "\n \\*_________________________________*\\\n";
dish::IncrementN();
}
void dish::ShowResult()
{
std::cout << "\n \\*________________________*\\\n";
std::cout << "\n THE NAME OF DISH: " << dish::GetName() << std::endl;
std::cout << " THE VARIETY: " << dish::GetVariety() << std::endl;
std::cout << " THE CALORIC CONTENT: " << dish::GetCaloric() << std::endl;
std::cout << " THE PRICE: " << dish::GetPrice() << std::endl;
std::cout << "\n \\*________________________*\\\n";
}
Implementation in the main...
#include <cstring>
using namespace std;
#include "dish.h"
int main() {
dish a;
a.ShowResult();
return 0;
}
Upvotes: 1
Views: 4563
Reputation: 1872
This is a syntax error which produces compile errors on dish.cpp . The linkage error is just a consequence of that.
Remove the static
keywords from dish.cpp .
Keep in mind that the only interesting error is almost always the very first one.
Explanation: in order to do class methods, write static
only in the header file, not in the .cpp .
For information but this is obviously not what you were trying to do: static
in a .cpp can be used to make a function invisible outside a compile unit, but is actually a deprecated construct inherited from C. You should prefer anonymous namespaces.
Upvotes: 0
Reputation: 337
You should link dish.cpp with your main executable i.e. add dish.cpp in your project.
Upvotes: 1