c++ - Unresolved template argument -
i testing template codes today and, find out interesting not find reason explain why happen. request consider , enlighten me knowledge. time.
this code block working without problem.
template<class titem> class printablequeue : public queue<titem> { public: friend ostream& operator<<(ostream& os, const printablequeue<titem>& queue) { copy(queue.c.begin(), queue.c.end(), ostream_iterator<titem>(os, " ")); return os; } }; int main(int argc, const char* argv[]) printablequeue<int> queue; queue.push(1); queue.push(2); cout << queue; }
however, when put definition of friend function outside of class, not work.
template<class titem> class printablequeue : public queue<titem> { public: friend ostream& operator<<(ostream& os, const printablequeue<titem>& queue); }; ostream& operator<<(ostream& os, const printablequeue<titem>& queue) { copy(queue.c.begin(), queue.c.end(), ostream_iterator<titem>(os, " ")); return os; }
the errors got below.
'titem' : undeclared identifier 'printablequeue' : 'titem' not valid template type argument parameter 'titem'
my question is, why compiler can not resolve titem ?
you need make function function template.
first change decleration of operator<< in class , include titem signature
friend ostream& operator<< <titem>(ostream& os, const printablequeue<titem>& queue);
than change function definition template function
template <typename titem> ostream& operator<<(ostream& os, const printablequeue<titem>& queue) { copy(queue.c.begin(), queue.c.end(), ostream_iterator<titem>(os, " ")); return os; }
Comments
Post a Comment