구조체가 'std :: cout'또는 다른 출력 스트림을 사용하여 출력하고 싶습니다. 클래스를 사용하지 않고 이것이 가능합니까?"<<"struct (no class) cout 스타일로 오버로드하기
감사
#include <iostream>
#include <fstream>
template <typename T>
struct point{
T x;
T y;
};
template <typename T>
std::ostream& dump(std::ostream &o,point<T> p) const{
o<<"x: " << p.x <<"\ty: " << p.y <<std::endl;
}
template<typename T>
std::ostream& operator << (std::ostream &o,const point<T> &a){
return dump(o,a);
}
int main(){
point<double> p;
p.x=0.1;
p.y=0.3;
dump(std::cout,p);
std::cout << p ;//how?
return 0;
}
나는 다른 구문을 시도 '하지만 난 그것을 작동하게하는 것 캔트.
"클래스를 사용하지 않음"은이 경우 특히 유용한 한정자가 아닙니다. struct가 public 멤버/상속을 기본적으로 제외하고 클래스는 기본적으로 private이므로 구조체와 클래스 간에는 기능상의 차이점이 없습니다. –