1

I have the following template class:

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

Then, in my main function, I do:

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

And I get the following error:

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

Any idea what I'm doing wrong?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126

2 Answers2

2

In this call

pool.return_resource(5);

a temporary object of type int with the value 5 is created as the function's argument.

A temporary object can not be bind with a non-constant reference.

Declare the function like

template <typename T> class ResourcePool {
    inline void return_resource( const T& instance) {
        /* do something */
    };
};
Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You are passing a temporary to a function that expect a reference. This bind can not be done. Try:

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

or

template <typename T> class ResourcePool {
    inline void return_resource(T instance) {  // <----
    /* do something */
    };
};
Amadeus
  • 10,199
  • 3
  • 25
  • 31