我一直在浏览StackOverflow,看起来尽管很多其他人遇到过与我类似的问题,但我似乎无法弄清楚我的问题是什么。
这是我第一次使用c ++。赋值是创建一个简单的向量类,分为3个文件。它也必须打包在命名空间中。
我的问题是,当我在终端中运行g++ -o test e_main.cpp
时,我收到错误:
Undefined symbols for architecture x86_64:
"e_vector::MyVector::add(e_vector::MyVector)", referenced from:
_main in e_main-eb4076.o
"e_vector::MyVector::norm()", referenced from:
_main in e_main-eb4076.o
"e_vector::MyVector::print()", referenced from:
_main in e_main-eb4076.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的" e_vector.h"文件:
namespace e_vector {
class MyVector {
public:
double x; // The X-component of vector
double y; // The Y-component of vector
double z; // The Z-component of vector
double norm();
void print();
MyVector add(MyVector u);
};
}
这是我的' functions.cpp'文件:
#include <iostream>
#include <cmath>
#include <string>
#include "e_vector.h"
using namespace std;
using namespace e_vector;
double e_vector::MyVector::norm() {
return (sqrt(x * x * y * y * z * z));
}
void e_vector::MyVector::print() {
cout << "(" << x << "," << y << "," << z << ")";
}
MyVector e_vector::MyVector::add(MyVector u) {
MyVector w;
w.x = x + u.x;
w.y = y + u.y;
w.z = z + u.z;
return w;
}
最后,这是我的&#34; e_main.cpp&#34;:
#include <iostream>
#include <cmath>
#include <string>
#include "e_vector.h"
using namespace std;
using namespace e_vector;
int main() {
MyVector v1 = {1.0, 1.0, 1.0};
MyVector v2 = {1.0, 0.0, 0.0};
MyVector v3;
cout << " norm of v1 = " << v1.norm(); // Test norm method
cout << " v1 = " ;
v1.print() ; // Test print method
cout << " v2 = " ;
v2.print() ;
v3 = v1.add(v2) ; // Test the add method
cout << " v3 = ";
v3.print();
return 0;
}
我是否可能错误地将文件链接在一起? 如果这显然很明显,请提前感谢,对不起!