![](/img/trans.png)
[英]I'm trying to make a menu in c++ and I don't understand why it doesn't loop
[英]I'm getting “undefined reference” errors and I don't understand why (C++ OO)
我看過其他許多關於未定義參考錯誤的文章,但是我的代碼中看不到任何錯誤。 有什么我沒捉到的嗎? 我正在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;
}
Object.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
Object.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 to
Object :: Object(double,double,double)的main': Main.cpp:(.text+0x47): undefined reference to
Main.cpp :(。text + 0x57):未定義對Object::printVolume()' Main.cpp:(.text+0x68): undefined reference to
引用Object::printVolume()' Main.cpp:(.text+0x68): undefined reference to
對Object::printVolume()' Main.cpp:(.text+0x68): undefined reference to
collect2:錯誤:ld返回1退出狀態
有什么我想念的嗎?
編譯似乎已成功,並且這些錯誤似乎是由鏈接器(或其他某種后編譯步驟)產生的,並且它們告訴您Object::Object(double xSize, double ySize, double zSize)
構造函數是無處可尋。
僅通過包含Main.cpp
Object.h
來讓編譯器知道您的對象是不夠的; 這將使編譯成功,但這只是故事的一半。
故事的另一半是連接也必須成功,所以你必須以某種方式使Object.o
提供給Main.o
鏈接過程中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.