我正在学习 C++,并接到了创建 Vector3D 类的作业。当我尝试在 OSX 上使用 G++ 编译 main.cpp 时,出现以下错误消息。为什么会这样?
g++ main.cpp
Undefined symbols for architecture x86_64:
"Vector3DStack::Vector3DStack(double, double, double)", referenced from:
_main in cc9dsPbh.o
ld: symbol(s) not found for architecture x86_64
main.cpp
#include <iostream>;
#include "Vector3DStack.h";
using namespace std;
int main() {
double x, y, z;
x = 1.0, y = 2.0, z = 3.0;
Vector3DStack v (x, y, z);
return 0;
}
Vector3DStack.h
class Vector3DStack {
public:
Vector3DStack (double, double, double);
double getX ();
double getY ();
double getZ ();
double getMagnitude();
protected:
double x, y, z;
};
Vector3DStack.cpp
#include <math.h>;
#include "Vector3DStack.h";
Vector3DStack::Vector3DStack (double a, double b, double c) {
x = a;
y = b;
z = c;
}
double Vector3DStack::getX () {
return x;
}
double Vector3DStack::getY () {
return y;
}
double Vector3DStack::getZ () {
return z;
}
double Vector3DStack::getMangitude () {
return sqrt (pow (x, 2) * pow (y, 2) * pow (z, 2));
}
最佳答案
您还必须编译和链接您的 Vector3DStack.cpp
。尝试:
g++ main.cpp Vector3DStack.cpp -o vectortest
这应该创建一个名为 vectortest
的可执行文件。
关于c++ - 体系结构 x86_64 的 G++ undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19430933/