2

I created a function template. It works fine if I use it in functions. Now, I'd like to use it inside a class but can't make the code compile:

#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;
}

The error I get is:

..\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&)'

Note: I must arrays since it is an interface for a USB driver.

0

1 Answer 1

2
Array2QList<>(frame, buffer, numBytes);

Here, numBytes is a const int, but Array2QList takes an int&. You can't bind a non-const reference to something that's const. It's unclear why you are taking that parameter by reference, so if you just pass by value instead, it'll work.

template <typename T>
void Array2QList(QList<T> &outList, const T in[], int insize){
//                                     get rid of &  ^
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It was indeed the const. I got confused since I have 2 functions with the same name but not the same parameters. The other one uses a reference to the size because the array is allocated inside the function. In the case I don't need the reference... But I wrongly kept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.