2012-02-22 82 views
1

有一个报头中的文件Rectangle.hxx错误而在C++创建头文件

#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或者我不需要?

+0

请将您的make文件代码 – 2012-02-22 06:35:50

+0

如何写入make文件代码 – 2012-02-22 06:42:45

+0

请告诉我们您用于构建程序的命令,该命令会导致您发布的错误消息。 – 2012-02-22 06:53:34

回答

2

您需要编译不先链接的不同文件。在UNIX编译器上,这通常是使用-c选项完成的。在构建可执行文件时,您可以指定所有生成的.o对象。或者,您可以一次性指定所有源文件,但对于非常小的项目来说,这确实是可行的。

+0

可以告诉我该怎么做?我正在Windows上使用cygwin – 2012-02-22 06:43:31

+0

只需使用'g ++ -c Rectangle.cxx'并且同样适用于其他源文件。最后,将所有文件放在一起:'g ++ -o program.exe Rectangle.o GetL.o main.o'(如果gcc在使用cygwin时会生成'.o'文件或'.obj',那么可能需要被改变)。为了减轻这种痛苦,你应该阅读'make'(或'gmake')并使用'Makefile'。 – 2012-02-22 06:53:08

0

您必须实施GetL::getWidth()。鉴于你的其他文件,你可能会在GetL.cxx中实现它。

+0

但我已经在Rectangle.cxx中实现了这个函数。 – 2012-02-22 07:34:21

+0

我在Rectangle.cxx中看到'Rectangle :: getWidth',但没有看到'GetL :: getWidth'。你还必须实现'GetL :: getWidth'。 – 2012-02-22 14:43:20