Ошибка при создании заголовочных файлов в C ++ - PullRequest
1 голос
/ 22 февраля 2012

Существует один заголовок файла Rectangle.hxx

#ifndef Rectangle_included
#define Rectangle_included
#include <iostream>
#include <math.h>
#include "GetL.hxx"
using namespace std;

class Rectangle: public GetL
{
int width;
int value;
public:
Rectangle();
Rectangle(int v, int w);
Rectangle(const Rectangle& b);
int getWidth();
int getValue();
Rectangle & plus(int newval);
};
#endif //Rectangle_included

Файл GetL.hxx определяется следующим образом:

#ifndef GetL_included
#define GetL_included
#include <iostream>
using namespace std;
class GetL
{
public:
virtual int getWidth();
};
#endif //GetL_include

Файл Rectangle.cxx содержит различные определения:

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

Rectangle::Rectangle()
{
 value=0;
 width=0;
}
Rectangle::Rectangle(int v, int w)
{
 value=v;
 width=w;
}
Rectangle::Rectangle(const Rectangle& b)
{
 value= b.value;
 width= b.width;
}
int Rectangle::getWidth()
{
 return width;
}
int Rectangle::getValue()
{
  return value;
}
Rectangle& Rectangle::plus(int newval)
{
  value+=newval;
  if(value>=pow(2,width))
cout<<"Overflow";
  return *this;
}

Но я получаю ошибку при компиляции Rectangle.cxx .

/tmp/cclETn3R.o:Rectangle.cxx:(.text$_ZN4GetLC2Ev[GetL::GetL()]+0*8): undefined reference to 'vtable for Getl'

Как я могу удалить это?Как я могу определить файл GetL.cxx или мне не нужно?

Ответы [ 2 ]

2 голосов
/ 22 февраля 2012

Вам необходимо скомпилировать разные файлы без предварительной ссылки.В компиляторах UNIX это обычно делается с помощью опции -c.При сборке исполняемого файла вы указываете все созданные .o объекты.В качестве альтернативы вы можете указать все исходные файлы одновременно, но это действительно только для очень маленьких проектов.

0 голосов
/ 22 февраля 2012

Вы должны реализовать GetL::getWidth(). Учитывая ваши другие файлы, вы, вероятно, реализуете его в GetL.cxx.

...