template class 를 받는 template func 오버로딩이 안됩니다.
글쓴이: leadha / 작성시간: 화, 2014/10/14 - 3:48오후
#include <iostream> #include <string> template <int I> class scalable; template <int I> std::ostream& operator<<(std::ostream&, scalable<I>&); template <int I> class scalable{ private: std::string str; int quotient; int remainder; public: scalable(int i){ quotient=i; while(quotient>=I){ remainder=quotient%I; quotient/=I; str=char(int('1')+remainder-1)+str; } str=char(int('1')+quotient-1)+str; } void print(){ std::cout << str << std::endl; } friend std::ostream& operator<<(std::ostream&, scalable<I>&); }; template <int I> std::ostream& operator<<(std::ostream& os, scalable<I>& num_str){ std::cout << num_str.str<<std::endl; return os; } ; int main(){ scalable<2> num(100); // num.print(); std::cout << num; getc(stdin); }
29 C:\Dev-Cpp\workspace\template_1.cpp [Warning] friend declaration `std::ostream& operator<<(std::ostream&, scalable&)' declares a non-template function
29 C:\Dev-Cpp\workspace\template_1.cpp [Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
[Linker error] undefined reference to `operator<<(std::ostream&, scalable<2>&)'
이렇게 세가지 오류가 뜨는데 이유를 모르겠네요.
num.print() 는 잘 되었던걸로 보아 template class 에는 문제가 없는것 같은데 조언 구하고자 글 올립니다.
Forums:
friend std::ostream&
저것도 해봤다고 생각했는데
아니었나보군요.. 잘 되네요. 감사합니다.
댓글 달기