gpt4 book ai didi

c++ - ld : symbol(s) not found

转载 作者:行者123 更新时间:2023-11-28 08:08:40 25 4
gpt4 key购买 nike

我现在正在学习 Ellis Horowitz 编写的 Fundamentals Of Data Structures in C++,试图实现第 77 页上的示例。但是,在我构建项目之后,Eclipse 控制台显示一些警告。

这是我的头文件:

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

另外,构建项目后会自动生成二进制文件吗?

最佳答案

您的 Rectangle 方法的实现确实缺失了。

这些是您在链接器错误消息中看到的方法:

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

如果您有一个 Rectangle.cpp(或 .cc、.cxx)文件,那么您还需要编译它并链接 Rectangle.o 文件。

既然你问了,这里有一个简单的概述,不同的文件名结尾是什么:

  • Rectangle.h 是包含类接口(interface)的头文件。通常,如果我阅读并理解此文件,使用其中定义的类就足够了。
  • Rectangle.cpp 是实现或源文件并包含实现。您也可以将它们放在 header 中,但对于较大的类,这会使 header 文件更加拥挤和一些其他缺点(编译时间速度、封装较少......)
  • Rectangle.o 是目标文件。这是编译器从头文件和源文件中生成的,并由链接器使用。

关于c++ - ld : symbol(s) not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9648150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com