c++ - 如何解决错误提示 "No matching function call for XXX"?

标签 c++ templates

为什么我会收到关于赋值行语句的“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/

相关文章:

C++ 在编译时用 constexpr char 数组指针分配静态数组?

c++ - 实现模板类接口(interface)

c++ - 解码运行时错误 (SIGFPE)

C++ 事件(焦点)处理

c++ - 访问 float 的 4 个字节是否会破坏 C++ 别名规则

c++ - 为类的特征特化实现错误消息

c++ - 在哪里声明枚举

c++ - 非常重要的模板 friend 声明

c++ - 大量使用移动平台模板

c++ - 如何实现一个同时支持栈和堆分配的类