Я попытался создать класс для трехмерного вектора в C++, но есть некоторые ошибки. Раньше я изучал немного oop в python, но я все еще довольно новичок в oop и C++. Я создал файл заголовка threevector.h, файл для класса threevector.cpp и основной файл программы main.cpp. Я просто хочу знать, что я сделал неправильно.ошибки при компиляции класса в C++
// threevector.h
#ifndef THREEVECTOR_H
#define THREEVECTOR_H
#include <iostream>
class threevector {
private:
double xcoord, ycoord, zcoord;
public:
threevector();
threevector(double x, double y, double z, char type);
void print();
};
#endif // THREEVECTOR_H
//threevector.cpp
#include "threevector.h"
#include <cmath>
threevector() {
xcoord = 0.0;
ycoord = 0.0;
zcoord = 0.0;
}
threevector(double x, double y, double z, char type) {
if (type == 'c') {
// cartesian coordinate
xcoord = x;
ycoord = y;
zcoord = z;
}
else if (type == 'p') {
// polar coordinate
// x = r, y = phi, z = theta
xcoord = x*sin(y)*cos(z);
ycoord = x*sin(y)*sin(z);
zcoord = x*cos(y);
}
}
void print() {
std::cout << xcoord << '\t' << ycoord << '\t' << zcoord << std::endl;
}
// main.cpp
#include "threevector.h"
#include <cmath>
#include <iostream>
using namespace std;
int main() {
threevector v0;
v0.print();
threevector v1(-1,2,4.384,'c');
cout << "v1 = ";
v1.print();
return 0;
}
Ниже приведены сообщения об ошибках я получаю:
main.cpp(.text+0x15): undefined reference to 'threevector::threevector()'
main.cpp(.text+0x15): undefined reference to 'threevector::print()'
main.cpp(.text+0x15): undefined reference to 'threevector::threevector::threevector(double, double, double, char)'
main.cpp(.text+0x15): undefined reference to 'threevector::print()'
[Error] ld returned 1 exit status
Я все еще получаю те же сообщения об ошибках даже после того, как добавил 3vector :: – Physicist