ld: símbolo (s) no encontrado

I'm now studying Fundamentals Of Data Structures in C++ escrito por Ellis Horowitz, trying to implement the example on the page 77. However, after I build the project, Eclipse Console shows up some warning.

Aquí está mi archivo de encabezado:

#ifndef RECTANGLE_H_
#define RECTANGLE_H_

class Rectangle{
public:
Rectangle();
~Rectangle();
int GetHeight();
int GetWidth();
private:
int xLow, yLow, height, width;
} ;

#endif

And here's my source file:

#include <iostream>
#include "Rectangle.h"
using namespace std;

int main(){
    Rectangle r, s;
    Rectangle *t = &s;

    if(r.GetHeight()*r.GetWidth() > t->GetHeight()*t->GetWidth())
        cout << "r";
    else
        cout << "s";
    cout << "has the greater area" << endl;

    return 0;
}

And the CDT Build Console shows:

Building target: rectangle
Invoking: MacOS X C++ Linker
g++  -o "rectangle"  ./main.o   
Undefined symbols:
  "Rectangle::Rectangle()", referenced from:
      _main in main.o
      _main in main.o
  "Rectangle::GetWidth()", referenced from:
      _main in main.o
      _main in main.o
  "Rectangle::GetHeight()", referenced from:
      _main in main.o
      _main in main.o
  "Rectangle::~Rectangle()", referenced from:
      _main in main.o
      _main in main.o
      _main in main.o
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [rectangle] Error 1

**** Build Finished ****

Besides, will binary files automatically created after building the project?

preguntado el 10 de marzo de 12 a las 16:03

You did not give any code for e.g. Rectangle constructor. -

2 Respuestas

The implementations to your Rectangle methods are really missing.

That are the methods, you see in the linker error messages:

Rectangle::Rectangle()
Rectangle::GetHeight()
Rectangle::GetWidth()    

Si usted tiene una Rectangle.cpp (or .cc, .cxx) file, than you need to compile this also and link the Rectangle.o file.

since you asked, here a simplified overview, what different filename ending are for:

  • Rectangle.h is the header file contains the Interface to your class. Usually it's enough if I read and understand this file, to use the classes that are defined there.
  • Rectangle.cpp is the implementation or source file and contains the implementation. You can put them also in the header but for larger classes this makes the header file more crowded and some other disadvantages (compile time speed, less encapsulation, ...)
  • Rectangle.o is the object file. This is what the compilador makes out of the header and source file and is used by the izquierda.

respondido 11 mar '12, 11:03

Thanks! I rewrote my code and set up constructor within class Rectangle: Rectangle(){height=10;width=5;}. It works! But I still have a question. What's the relation between .o, .h and .cpp? - lucasKo

You haven't defined the Rectangle class functions anywhere. Where is Rectangle.c?

The header file simply declares that the class exists, but you have provided no definitions for that class. You need a Rectangle.c that does that. Also you will have to link with Rectangle.o.

respondido 10 mar '12, 16:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.