C ++에서 3 차원 벡터에 대한 클래스를 만들려고했지만 몇 가지 오류가 있습니다. 나는 전에 파이썬에서 약간의 oop을 배웠지 만 여전히 oop 및 C ++에 익숙하지 않습니다. 헤더 파일 threevector.h, 클래스 threevector.cpp 파일 및 메인 프로그램 파일 main.cpp를 만들었습니다. 나는 내가 뭘 잘못했는지 알고 싶다.
// 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
threevector
의 방법을 잘못 정의했습니다 . 그것은해야한다:
threevector::threevector() {
xcoord = 0.0;
ycoord = 0.0;
zcoord = 0.0;
}
threevector::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 threevector::print () {
std::cout << xcoord << '\t' << ycoord << '\t' << zcoord << std::endl;
}
의 모든 threevector
메서드는 threevector
의 범위에 있습니다.
다음과 같이 컴파일하는 것을 잊지 마십시오 main.cpp
.
g++ threevector.cpp main.cpp
이것은 threevector
동일한 개체 파일에있는 것처럼 제대로 연결 됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다