I've been going through StackOverflow, and it seems that although many others have had a similar issue to mine, I can't seem to work out what my problem is.
This is my first time working with c++. The assignment is to make a simple vector class, separated across 3 files. It also must be packaged in a namespace.
My issue is that when I run g++ -o test e_main.cpp
in Terminal, I get the error:
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)
Here is my "e_vector.h" file:
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);
};
}
Here is my 'functions.cpp' file:
#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;
}
Finally, here is my "e_main.cpp":
#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;
}
Am I perhaps linking the files together incorrectly? Thank you in advance and sorry if this is blatantly obvious!