【发布时间】:2017-06-15 17:46:49
【问题描述】:
我查看了有关未定义引用错误的多个其他帖子,但在我的代码中看不到任何错误。有什么我没抓到的吗?我在 ubuntu 命令行中使用 g++ 编译。
这是我的代码和来自终端的错误:
Main.cpp:
#include <iostream>
#include "Object.h"
using namespace std;
int main(){
Object* o = new Object(3,6,9);
o->printVolume();
delete o;
return 0;
}
对象.h:
#ifndef OBJECT_H_
#define OBJECT_H_
class Object
{
public:
Object(double xSize, double ySize, double zSize);
~Object();
void printVolume();
private:
double x,y,z;
};
#endif
对象.cpp:
#include <iostream>
#include "Object.h"
using namespace std;
Object::Object(double xSize, double ySize, double zSize){
x = xSize;
y = ySize;
z = zSize;
}
Object::~Object(){
cout << "Object destroyed." << endl;
}
void Object::printVolume(){
cout << x * y * z << endl;
}
错误:
/tmp/ccUeuPTn.o: 在函数main':
Main.cpp:(.text+0x47): undefined reference toObject::Object(double, double, double)'
Main.cpp:(.text+0x57): 未定义引用Object::printVolume()'
Main.cpp:(.text+0x68): undefined reference toObject::~Object()'
collect2:错误:ld 返回 1 个退出状态
我有什么遗漏的吗?
【问题讨论】:
-
你是怎么编译的?
-
终端中的g++
-
我运行这个:
~/Desktop/C++ Projects/Forge/src$ g++ -I../include Main.cpp -o Main.o -
你忘记编译和链接
Object.cpp。 -
你如何将 Object.o 链接到你的主目录?
标签: c++ oop undefined-reference