c++ - Fail to use function template with array as parameter inside a class -
i created function template. works fine if use in functions. now, i'd use inside class can't make code compile:
#include <qlist> // function template template <typename t> void array2qlist(qlist<t> &outlist, const t in[], int &insize) { (int = 0; < insize; ++i) outlist.append(in[i]); } // class using template class parser { public: parser(const unsigned char buffer[], const int numbytes){ array2qlist<>(frame, buffer, numbytes); // 1 fails } ~parser(){} qlist<unsigned char> frame; }; // main int main(int argc, char *argv[]) { int size = 50; unsigned char buffer[size]; qlist<unsigned char> frame; array2qlist<unsigned char>(frame, buffer, size); // 1 works parser parser = parser(buffer, size); return 0; }
the error is:
..\sandbox\main.cpp: in constructor 'parser::parser(const unsigned char*, int)':
..\sandbox\main.cpp:20: error: no matching function call 'array2qlist(qlist&, const unsigned char*&, const int&)'
note: must arrays since interface usb driver.
array2qlist<>(frame, buffer, numbytes);
here, numbytes
const int
, array2qlist
takes int&
. can't bind non-const
reference that's const
. it's unclear why taking parameter reference, if pass value instead, it'll work.
template <typename t> void array2qlist(qlist<t> &outlist, const t in[], int insize){ // rid of & ^
Comments
Post a Comment