Fail to use function template with array as parameter inside a class
我创建了一个函数模板。如果我在函数中使用它,它就可以正常工作。现在,我想在类中使用它,但不能编译代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <QList> // Function template template <typename T> void Array2QList(QList<T> &outList, const T in[], int &insize) { for (int i = 0; i < insize; ++i) outList.append(in[i]); } // Class using the template class Parser { public: Parser(const unsigned char buffer[], const int numBytes){ Array2QList<>(frame, buffer, numBytes); // This one 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); // This one works Parser parser = Parser(buffer, size); return 0; } |
我得到的错误是:
..\SandBox\main.cpp: In constructor 'Parser::Parser(const unsigned
char*, int)':..\SandBox\main.cpp:20: error: no matching function for
call to 'Array2QList(QList&, const unsigned char*&, const int&)'
注意:我必须阵列,因为它是USB驱动程序的接口。
1 | Array2QList<>(frame, buffer, numBytes); |
这里,
1 2 3 | template <typename T> void Array2QList(QList<T> &outList, const T in[], int insize){ // get rid of & ^ |