i have class constructor has initialised template class arguments. now, need pass constructor of class method in main()
. trying so, throws error , not able solution it.
template<class k> class student { friend void printfunction(); public: student(k val) {std::cout<<val;} }; void printfunction(student object) { ...... } int main() { student <int> object(10); printfunction(object); //this line throws error } error: use of class template 'student' requires template arguments
there several problems code. first , easy 1 pass class name student
call of printfunction
. should call printfunction(object)
instead.
second, printfunction
accept instance of template class student
, need make template well. see working example below.
#include <iostream> template<class k> class student; template<class k> void printfunction(student<k> object) { std::cout << object.val; } template<class k> class student { friend void printfunction<>(student<k>); public: student(k val) : val(val) {} private: k val; }; int main() { student<int> object(10); printfunction(object); }
Comments
Post a Comment