Justin Jin
Justin Jin

Reputation: 1

How to resolve error says "No matching function call for XXX"?

Why did I get "No matching function for call to 'getVector'" error about the assignment line statement?

template <typename T>
vector<T> getVector(int);

int main() {
    auto myVector = getVector(5);
    ...
}

template <typename T>
vector<T> getVector(int size) {
    ...
}

Upvotes: 0

Views: 446

Answers (2)

user703016
user703016

Reputation: 37945

Look at what your compiler says! It's trying to help you.

main.cpp:7:21: error: no matching function for call to 'getVector'

And then:

main.cpp:4:16: note: candidate template ignored: couldn't infer template argument 'T' std::vector<T> getVector(int);

The error is very clear: the compiler sees your getVector function, but you never mentioned a "concrete" type to substitute for T: the compiler has no idea what you want the vector to hold, so it simply ignores that function template.

What kind of values do you want to store in the vector? Integers for example? Then:

auto myVector = getVector<int>(5);
                          ^ give a type here

Upvotes: 3

David G
David G

Reputation: 96790

You defined getVector() as a function template that takes a template argument T. That template argument needs to be provided somehow. You can't call the function without T either being deduced or explicitly provided.

For example, if the vector you are returning holds integers, you can provide int for T like this:

auto myVector = getVector<int>(5);

Upvotes: 0

Related Questions