简体   繁体   中英

couldn't deduce template parameter ‘U

I have a following function I want to use:

template <typename T>   
template <typename U>
TypedData<U>* convert(T min, T max) const;

Here's how I used it in the code:

const TypedData<unsigned short>* B = A->convert((float)0,(float)65535);

where A is the following type:

const TypedData<float>*

The complier gives me the following error and notes:

error: no matching function for call to ‘TypedData<float>::convert(float, float) const’
note: candidate: template<class U> TypedData<U>* TypedData<T>::convert(T, T) const [with U = U; T = float]
   TypedData<U>* convert(T min, T max) const;
note:   template argument deduction/substitution failed:
note:   couldn't deduce template parameter ‘U

I want to ask if I am using this template function correctly? and what is the reason that it gives me the error. Thanks.

You need to provide the U template parameter to convert as well:

const TypedData<unsigned short>* B = 
    A->convert<unsigned short>((float)0,(float)65535);

Note that this means you can avoid specifying the template parameter on the left hand side:

const auto* B = 
    A->convert<unsigned short>((float)0,(float)65535);

Here's a demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM