【发布时间】:2021-01-02 08:06:27
【问题描述】:
我是 C++ 的完全初学者。我正在通过object oriented programming Data structures in c++ 学习 C++。在课程中我有以下程序
Cube.h
#pragma once
class Cube {
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
Cube.cpp
#include "Cube.h"
double Cube::getVolume() {
return length_ * length_ * length_;
}
double Cube::getSurfaceArea() {
return 6 * length_ * length_;
}
void Cube::setLength(double length) {
length_ = length;
}
main.cpp
#include <iostream>
#include "Cube.h"
int main() {
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << "Volume" << volume << std::endl;
return 0;
}
当我使用make main 制作这个程序时,我收到以下错误消息
c++ main.cpp -o main
Undefined symbols for architecture x86_64:
"Cube::getVolume()", referenced from:
_main in main-6c5fe0.o
"Cube::setLength(double)", referenced from:
_main in main-6c5fe0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
不知道我做错了什么。我正在使用 Macbook 工作。我跟着这个link在mac中运行cpp程序。
不知道我做错了什么。对错误的解释也很好。
提前致谢
【问题讨论】:
-
你好,你也应该编译Cube.cpp,比如c++ *.cpp -o main。
-
this question 上的答案很有趣。出于某种原因,所有人都假设您编译单个源文件并且不适用于您的情况
-
@idclev463035818 抱歉我的无知。正如我在问题中解释的那样,我是完全的初学者,还不了解错误。现在已经学会了
-
@Sashaank 无需抱歉。也许答案的作者应该是;)
标签: c++