+ 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).
2 Réponses
+ 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);
}
+ 3
Hatsy Rei The code works again! Thank you very much!