c++ - 使用 g++ 编译器的架构 arm64 的未定义符号
问题描述
我试图让一些代码工作,但无论编译器如何,我都会遇到同样的错误。我正在尝试使运算符超载,但出现错误。
我有 3 个文件:main.cpp、vector2d.cpp 和 vector2d.h
这是我在使用 g++ 编译器时遇到的错误:
Undefined symbols for architecture arm64:
"v2d::length()", referenced from:
_main in main-c7db92.o
"v2d::v2d(v2d const&)", referenced from:
_main in main-c7db92.o
"v2d::v2d(double, double)", referenced from:
_main in main-c7db92.o
"v2d::~v2d()", referenced from:
_main in main-c7db92.o
"v2d::operator=(v2d const&)", referenced from:
_main in main-c7db92.o
"v2d::operator*(double)", referenced from:
_main in main-c7db92.o
"v2d::operator+(v2d const&)", referenced from:
_main in main-c7db92.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的 main.cpp
#include <iostream>
#include "vector2d.h"
using namespace std;
int main() {
// We crate some vectors with fixed values
v2d v1(3,0); //(3,0)
v2d v2(0,4);
v2d v3(3,2);
// We create v4 as vector which is like v2
v2d v4(v2);
cout << "Pythagoras holds on perpendicular triangles (a,b,c):" << endl;
cout << "a = " << v1.length();
cout << " , b = " << v2.length();
// We create a new vector v5 by combining other vectors
// This vector corresponds to the diagonal of the triangle defined by v1 and v2
v2d v5 = v1 + v2 * (-1);
cout << " , c = " << v5.length() << endl;
cout << "...but not on non-perpendicular triangles (a,b,c):" << endl;
cout << "a = " << v3.length();
cout << " , b = " << v4.length();
v5 = v3 + v4 * (-1);
cout << " , c = " << v5.length() << endl;
return 0;
}
这是我的vector2d.cpp
#include "vector2d.h"
#include <cmath>
v2d::v2d(double a, double b) {
// Write your code here
}
v2d::v2d(const v2d & v) {
// Write your code here
}
v2d::~v2d() {
// Write your code here
}
v2d & v2d::operator=(const v2d &v) {
// Write your code here
return *this;
}
v2d & v2d::operator+(const v2d &v) {
// Write your code here
return *this;
}
double v2d::operator*(const v2d &v) {
// Write your code here
return 0;
}
v2d & v2d::operator*(double k) {
// Write your code here
return *this;
}
double v2d::length() {
// Write your code here
return 0;
}
这是我的vector2d.h
#ifndef __v2d__
#define __v2d__
class v2d {
public:
// Standard constructor: builds a vector (a,b)
v2d(double a, double b);
// Copy constructor: builds a vector that is exactly as v
v2d(const v2d & v);
// Destructor
~v2d(void);
// Assignment operator: updates the vector to make it as v
v2d & operator=(const v2d &v);
// Vector addition: updates the vector by adding v
v2d & operator+(const v2d &v);
// Scalar multiplication: updates the vector by scaling by k
v2d & operator*(double k);
// Scalar product of the current vector by another vector v
double operator*(const v2d &v);
// Returns the length of a vector
double length(void);
private:
// Internal representation of a vector with just two doubles x and y
double x;
double y;
};
#endif
我真的卡住了...
解决方案
我的猜测是你错过了添加vector2d.cpp
到你的项目中。添加后(使用您使用的任何构建系统)错误应该消失。
这些错误表明编译器试图在不存在该文件中的符号的情况下链接程序。
我无法确切说明如何添加它,因为它没有在项目是如何构建的问题中指定,但是如果您只想从终端编译它可能就像
g++ main.cpp vector2d.cpp -o program_name
## ^---- this was probably missing