2012-03-10 67 views
0

现在我正在学习基础数据结构在C++通过埃利斯霍洛维茨写的,想实现的例子77页。但是,当我生成项目时,Eclipse控制台显示一些警告。LD:符号(S)没有发现

这是我的头文件:

#ifndef RECTANGLE_H_ 
#define RECTANGLE_H_ 

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

#endif 

这是我的源文件:

#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; 
} 

而且CDT构建控制台显示:

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 **** 

此外,将二进制文件自动创建在建立项目之后?

+0

您没有给出任何代码。 '矩形'构造函数。 – 2012-03-10 16:40:41

回答

1

您的Rectangle方法的实现真的缺失。

这是方法,您在连接的错误信息,请参阅:

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

如果你有Rectangle.cpp(或.cc的,.CXX)文件,比你也需要这个编译并链接Rectangle.o文件。

既然你问,这里一个简单的概述,有哪些不同的文件名结尾为:

  • Rectangle.h是头文件包含接口上您的课。通常,如果我阅读并理解这个文件,就足够使用在那里定义的类。
  • Rectangle.cpp是实现或源文件并包含实现。你也可以把它们放在头文件中,但是对于较大的类,这会使头文件更加拥挤并带来一些其他缺点(编译时速度,封装更少......)
  • Rectangle.o是对象文件。这就是编译器所产生的头文件和源文件,并被连接器使用。
+0

谢谢!我重写了我的代码并在类Rectangle中设置了构造函数:Rectangle(){height = 10; width = 5;}。有用!但我仍然有一个问题。 .o,.h和.cpp之间的关系是什么? – lucasKoTW 2012-03-11 02:44:25

0

您还没有在任何地方定义Rectangle类的功能。 Rectangle.c在哪里?

头文件只是声明该类存在,但您没有提供该类的定义。你需要一个Rectangle.c来做到这一点。你也必须链接到Rectangle.o。