ठीक है, मैंने सोचा पढ़ा काफी कुछ सवाल "टेम्पलेट तर्क अनुमान नहीं कर सकता है" लेकिन कोई भी मेरे मामले मिलान करने के लिए लगता है - या मैं इस सवाल का जवाब समझ में नहीं आता ...बात का अनुमान लगाना टेम्पलेट तर्क विफल रहता है
वहाँ one है कि मैं महसूस सही दिशा में चला जाता है, लेकिन मैं अपनी समस्या के समाधान को निकालने में असफल रहा।
मेरी शीर्षक में कोड नीचे छीन इस तरह दिखता है:
template<typename T>
class TemplateProblem
{
public:
// Do I really need this or did I miss something from the STL?
template<typename Tin, typename Tout>
struct UnaryFunction : public std::unary_function<Tin, Tout>
{
virtual Tout operator()(Tin input) = 0;
};
template<typename Tin, typename Tout>
struct StaticCast : public UnaryFunction<Tin, Tout>
{
virtual Tout operator()(Tin input)
{
return static_cast<Tout>(input);
}
};
private:
T * const _data;
T const _bias;
template<typename Tin>
void Init(Tin * data, int length, UnaryFunction<Tin, T> mapper, Tin bias);
public:
template<typename Tin>
TemplateProblem(Tin * data, int length, Tin bias = Tin());
template<typename Tin>
TemplateProblem(Tin * data, int length, UnaryFunction<Tin, T> mapper, Tin bias = T());
};
template<typename T>
template<typename Tin>
void TemplateProblem<T>::Init(Tin * data, int length, UnaryFunction<Tin, T> mapper, Tin bias)
{
T mappedBias = mapper(bias);
for (int i = 0; i < length; i++)
{
_data[i] = data[i] + mappedBias;
}
}
template<typename T>
template<typename Tin>
TemplateProblem<T>::TemplateProblem(Tin * data, int length, UnaryFunction<Tin, T> mapper, Tin bias = T())
: _data(new T[length]), _bias(bias)
{
Init(data, length, mapper, bias);
}
template<typename T>
template<typename Tin>
TemplateProblem<T>::TemplateProblem(Tin * data, int length, Tin bias = T())
: _data(new T[length]), _bias(bias)
{
StaticCast<Tin, T> cast;
Init(data, length, cast, bias);
}
मैं इस तरह यह दृष्टांत:
Error 1 error C2784: 'void TemplateProblem<T>::Init(Tin *,int,TemplateProblem<T>::UnaryFunction<Tin,T>,Tin)' : could not deduce template argument for 'TemplateProblem<T>::UnaryFunction<Tin,T>' from 'TemplateProblem<T>::StaticCast<Tin,Tout>' ...\templateproblem.h 62 1 TemplateProblem
Error 2 error C2893: Failed to specialize function template 'void TemplateProblem<T>::Init(Tin *,int,TemplateProblem<T>::UnaryFunction<Tin,T>,Tin)' ...\templateproblem.h 62 1 TemplateProblem
:
unsigned char pixels[] = {23, 42, 65, 97};
TemplateProblem<int> tp(pixels, 4);
VS2012 से मैं इन संदेशों को प्राप्त त्रुटि तब भी होती है जब मैं class
केके रूप में दो struct
एस को स्थानांतरित करता हूंसुझाव देता है।
तत्काल कोड और त्रुटि संदेश पूरी तरह से असंबंधित हैं। त्रुटि 'स्टेटिककास्ट' के बारे में वार्तालाप करती है, लेकिन आपके पास अपने तत्काल कोड में कहीं भी नहीं है। –
Xeo
http://liveworkspace.org/code/4psoUf$0 ठीक संकलित करता है ... आपका कोड (सुधार से पहले) संकलित नहीं था। – ForEveR
@Xeo: StaticCast नीचे से 3 लाइनों का उपयोग किया जाता है। – primfaktor