+ 5

[SOLVED] Writing friend functions outside template classes

I have the following class : template<class M> class Matrix; I defined the following friend function that prints multiple objects of this class in the same line, inside the class definition : https://code.sololearn.com/cPQLM3taj4Q6/#cpp The function works just as intended. But I now want to move the definition outside the class. How should I rewrite the function header? I removed the friend keyword, but that resulted in errors like : class template argument deduction failed for Matrix max_m, etc. Even replacing template<class.. Args> with template<class M,class... Args> failed (resulted in undefined references to sync_print).

4th Oct 2018, 3:14 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
2 Answers
+ 8
I'm not sure if this helps, but: template <class M> class Matrix { private: M member; template <class N, class... Args> friend void sync_print(Matrix<N> &obj); }; template <class M, class... Args> void sync_print(Matrix<M> &obj) { std::cout << obj.member; } int main() { Matrix<int> obj; // sync_print<int>(obj); }
6th Oct 2018, 3:13 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Hatsy Rei The code works again! Thank you very much!
7th Oct 2018, 2:56 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar