C++ 模板类 : error: no matching function for call to

标签 c++ templates pass-by-reference function-declaration pass-by-rvalue-reference

我有以下模板类:

template <typename T> class ResourcePool {
    inline void return_resource(T& instance) {
        /* do something */
    };
};

然后,在我的ma​​in 函数中,我会:

ResoucePool<int> pool;
pool.return_resource(5);

我收到以下错误:

error: no matching function for call to `ResourcePool<int>::return_resource(int)`

知道我做错了什么吗?

最佳答案

在这次通话中

pool.return_resource(5);

创建了一个值为 5 的 int 类型的临时对象作为函数的参数。

临时对象不能与非常量引用绑定(bind)。

像这样声明函数

template <typename T> class ResourcePool {
    inline void return_resource( const T& instance) {
        /* do something */
    };
};

关于C++ 模板类 : error: no matching function for call to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44305592/

相关文章:

php - 单个元素的引用行为与数组容器的引用状态分离?

c++ - 构造函数中的字段初始化破坏内存

c++ - 将奇怪的符号打印到输出文件,C++

带有引用参数的 C++ 模板隐式实例化

c++ - 这是通过引用传递文件指针的正确语法吗?

c++ - 使用 std::pairs 数组初始化 std::map 问题(指针错误?)

c++ - 必须包含所有 "file.o"文件,其中有 "#include "file.h""

c# - 如何在 C# 中创建具有动态属性和属性的结构

c++ - 模板特化和从其他模板类继承模板类

c++ - 是否有可能避免有关 void 函数返回值的 xrefwrap 错误?