对 std::ios_base::Init::Init() 的未定义引用
undefined reference to std::ios_base::Init::Init()
我正在努力与C++一起学习OOP,但遇到了问题。我确定这是一个内存分配问题,但似乎无法解决它。任何帮助将不胜感激。
我的客户端代码
#include <iostream>
#include "Box.cpp"
using namespace std;
int main(){
Box *box = new Box;
return 0;
}
我的盒子课...
#include <iostream>
using namespace std;
class Box{
private:
double width;
double height;
double perimeter;
double area;
public:
Box(){
cout << "Box created" << endl;
}
~Box(){
cout << "Box Destroyed" << endl;
}
double getWidth(){
//
return this->width;
}
double getHeight(){
//
return this->height;
}
double getArea(){
//
return this->area;
}
double getPerimeter(){
//
return this->perimeter;
}
void setWidth(double w){
//
this->width = w;
if(!this->height){
computeSetArea(this->width, this->height);
computeSetPerimeter(this->width, this->height);
}
}
void setHeight(double h){
//
this->height = h;
if(!this->width){
computeSetArea(this->width, this->height);
computeSetPerimeter(this->width, this->height);
}
}
private:
void computeSetArea(double w, double h){
//
this->area = w*h;
}
void computeSetPerimeter(double w, double h){
//
this->perimeter = (w * 2) + (h + 2);
}
};
我使用 gcc 并执行:
gcc Box.cpp client.cpp -o mainfile
在此之后,我收到此错误。
/tmp/ccaVb21k.o: In function `__static_initialization_and_destruction_0(int, int)':
Box.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
Box.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccaVb21k.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
/tmp/ccjtbzi4.o: In function `main':
client.cpp:(.text+0x14): undefined reference to `operator new(unsigned int)'
client.cpp:(.text+0x3f): undefined reference to `operator delete(void*)'
/tmp/ccjtbzi4.o: In function `__static_initialization_and_destruction_0(int, int)':
client.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::Init()'
client.cpp:(.text+0x71): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccjtbzi4.o: In function `Box::Box()':
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x11): undefined reference to `std::cout'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x16): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccjtbzi4.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
/tmp/ccjtbzi4.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
你的代码编译得很好,你得到一个链接器错误(ld
是链接器,它返回1(错误)),抱怨缺少c ++库。
要解决此问题,您需要将 stdc++ 库添加到命令行,或使用 g++。
将gcc
替换为 g++
或将-lstdc++
添加到 gcc 命令行。
gcc Box.cpp client.cpp -o mainfile -lstdc++
或
g++ Box.cpp client.cpp -o mainfile
这会将 std c++ 库与编译的代码链接起来。使用 g++
,可以省略此步骤。
像这样设置你的类结构对我有用:
方框:
class Box
{
public:
Box();
~Box();
double getWidth();
double getHeight();
double getArea();
double getPerimeter();
void setWidth(double w);
void setHeight(double h);
void computeSetArea(double w, double h);
void computeSetPerimeter(double w, double h);
private:
double width;
double height;
double perimeter;
double area;
};
然后对于Box.cpp:
#include "box.h"
#include <iostream>
using namespace std;
Box::Box(){
cout << "Box created" << endl;
}
Box::~Box(){
cout << "Box Destroyed" << endl;
}
double Box::getWidth(){
return this->width;
}
double Box::getHeight(){
return this->height;
}
double Box::getArea(){
return this->area;
}
double Box::getPerimeter(){
return this->perimeter;
}
void Box::setWidth(double w){
this->width = w;
if(!this->height){
computeSetArea(this->width, this->height);
computeSetPerimeter(this->width, this->height);
}
}
void Box::setHeight(double h){
this->height = h;
if(!this->width){
computeSetArea(this->width, this->height);
computeSetPerimeter(this->width, this->height);
}
}
void Box::computeSetArea(double w, double h){
this->area = w*h;
}
void Box::computeSetPerimeter(double w, double h) {
this->perimeter = (w * 2) + (h + 2);
}
输出:
Box created
尝试使用 g++ 而不是 gcc。
相关文章:
- 编译C++时未定义的引用
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 不知道某个东西是否被忽略会引入未定义的行为吗
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- c++11评估顺序(未定义的行为)
- 使用mysql c++连接器的未定义引用
- 从python调用openMP共享库时,未定义opnMP函数
- 在 Mac 上使用 CMAKE 将 FFTW 和 FFTWPP 链接到项目中时未定义的符号
- Cmake 链接问题:未定义对 Button::mousePressEvent(QGraphicsSceneMouseE
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- Android NDK 未定义对 'std::ios_base::Init::Init()' 的引用
- 使用 CMake 对 boost::iostreams::mapped_file_source::init() 的未定义
- 对"Init::b"的未定义引用
- 对 std::ios_base::Init::Init() 的未定义引用
- gcc 未定义对 'std::ios_base::Init::Init()' 的引用