1

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;
} 
hiken
  • 69
  • 1
  • 8
  • 2
    [Failure to link against appropriate libraries/object files or compile implementation files](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – chris Apr 22 '13 at 19:15
  • Can you show how you compiled this code? – lucasmrod Apr 22 '13 at 19:17
  • I did it either without creating a project and within the project... – hiken Apr 22 '13 at 19:36
  • I'm pretty sure most of the lines of code above are not related to the problem and can be deleted from the question. Please do so, and in the process you may discover the source of your problem. – Raymond Chen Apr 22 '13 at 20:55

2 Answers2

1

You should link dish.cpp with your main executable i.e. add dish.cpp in your project.

43l0v3k
  • 337
  • 2
  • 9
  • Do you use any particular environment for compiling, like Eclipse or KDevelop? If yes there should be a place where you declare the list of .cpp files you want to compile. Each .cpp file has to be compiled and produces a .o file. Afterwards the .o files are linked to produce the binary. Obviously only main.cpp gets compiled but not dish.cpp . If you use a well known environment, you probably can find plenty of examples on the web. – Aurélien Apr 22 '13 at 19:52
  • To be sure you can even add a syntax error in dish.cpp and you will see that you compiler doesn't even complain about that, because it doesn't even try to compile it. – Aurélien Apr 22 '13 at 19:57
  • I'm using Dev-C++ 4.9.9.2 compiler, I've created new project added there dish.cpp and header file and executed main but it is useless – hiken Apr 22 '13 at 19:58
  • There is another possibility: maybe there is a syntax error in dish.cpp, thus dish.cpp doesn't get compiled, thus you get at the end the link error. Are you sure that the link error is the very first error? – Aurélien Apr 22 '13 at 20:01
  • Do you want me to compile dish.cpp? – hiken Apr 22 '13 at 20:05
  • Actually, it shows error when compiling the dish.cpp it says: – hiken Apr 22 '13 at 20:07
  • [cannot declare member functions 'static int dish::GetN()' to have static linkage – hiken Apr 22 '13 at 20:08
0

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.

Aurélien
  • 1,742
  • 1
  • 19
  • 30
  • I did as you guided but it is again useless – hiken Apr 22 '13 at 20:18
  • Well now you may have another error, and another, and another. Fix each one of them and it will work. Focus each time on the very first error. Good luck. – Aurélien Apr 22 '13 at 20:20
  • You're welcome and if you block on another error, post another question (one specific problem = one specific question). This specific question concerning your linkage error is now solved. You should mark one of the answers as correct. Have fun at learning C++ :) – Aurélien Apr 22 '13 at 20:24
  • Okay, then... I've removed 'static' in dish.cpp but not in dish.h, but now it's viewing another error [linker error] undefined error to 'WinMain@16' compiling the dish.cpp – hiken Apr 22 '13 at 20:32
  • I'm not confident with your environment but it looks like you are compiling separately dish.cpp from main.cpp, like it's trying to make one binary for each of them, thus it doesn't find any main() in dish.cpp . I would say there is now a problem in the way your project is configured as it should not try to produce two binaries but rather two object files (.o) and link them together into a binary (.exe). It has nothing to do anymore with the title of your question so you really should post another question now. Good luck. – Aurélien Apr 22 '13 at 20:37
  • and if I look at your files I can find plenty of other mistakes: `int dish::N;` has nothing to do in a header. Semantically, `GetN()` and `IncrementN()` shouldn't be class methods. You call several methods by doing `dish::method();` which is useless. You write `using namespace std;` in main.cpp but you do nothing with the `std` there. It looks like you are lacking the basics of C++. You should read some tutorials before continuing otherwise you will lose a lot of time at trying to understand why all of this doesn't work... – Aurélien Apr 22 '13 at 20:43
  • Sorry, but static members ought to be declared outside the class, while defined within a class, as well. Concerning the 'dish::method()' I haven't used such method, I declared methods by specific name and defined them in dish.cpp – hiken Apr 22 '13 at 20:50
  • yes but `int dish::N;` must be in the .cpp, not in the header, otherwise it will appear both in dish.o and main.o and the linker will tell you `error, multiple definition of N`. – Aurélien Apr 22 '13 at 20:52
  • It didn't change anything... by the way, why do you think that GetN() and IncrementN() shouldn't be class methods? – hiken Apr 22 '13 at 20:57
  • I really want to help you but if we continue to chat here we will get into troubles as it's not the spirit of StackOverflow. In my profile you can find my e-mail address. Send me a mail. on my side I'll modify your files to make them at least compilable. – Aurélien Apr 22 '13 at 21:01
  • Sorry, but it's not visible... my email is apogeedecielo@gmail.com – hiken Apr 22 '13 at 21:06