为什么我会收到关于赋值行语句的“No matching function for call to 'getVector'”错误?
template <typename T>
vector<T> getVector(int);
int main() {
auto myVector = getVector(5);
...
}
template <typename T>
vector<T> getVector(int size) {
...
}
最佳答案
看看你的编译器怎么说!它正在努力帮助您。
main.cpp:7:21: error: no matching function for call to 'getVector'
然后:
main.cpp:4:16: note: candidate template ignored: couldn't infer template argument
'T' std::vector<T> getVector(int);
错误很明显:编译器看到了你的getVector
函数,但您从未提到用“具体”类型代替 T
: 编译器不知道您希望 vector 包含什么,因此它会忽略该函数模板。
你想在 vector 中存储什么样的值?例如整数?然后:
auto myVector = getVector<int>(5);
^ give a type here
关于c++ - 如何解决错误提示 "No matching function call for XXX"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26152413/