मैं फ़ंक्शन लिखने की कोशिश कर रहा हूं जो दो वैक्टरों के लिए स्केलर उत्पाद की गणना करता है। यहां कोड है और यह काम करता है।कई प्रकार के साथ रिकर्सिव टेम्पलेट फ़ंक्शन
template <int N>
int scalar_product (std::vector<int>::iterator a,
std::vector<int>::iterator b) {
return (*a) * (*b) + scalar_product<N - 1>(a + 1, b + 1);
}
template <>
int scalar_product<0>(std::vector<int>::iterator a,
std::vector<int>::iterator b) {
return 0;
}
लेकिन यहाँ समस्या है - मैं, टेम्पलेट प्रकार के साथ इस iterators बदलना चाहते हैं तो समारोह के हस्ताक्षर इस
template <typename Iterator ,int N>
int scalar_product (Iterator a, Iterator b) {
return (*a) * (*b) + scalar_product<N - 1>(a + 1, b + 1);
}
template <typename Iterator>
int scalar_product<0>(Iterator a,
Iterator b) {
return 0;
}
तरह ख़ाली दिखेगा लेकिन यह काम नहीं करता है - मैं संकलन मिल त्रुटि C2768: स्पष्ट टेम्पलेट तर्कों का अवैध उपयोग। यह मूर्खतापूर्ण प्रतीत होता है, लेकिन मुझे यह नहीं पता था कि इस त्रुटि से बचने के लिए मुझे क्या बदलना चाहिए।
देखें इस: http://stackoverflow.com/questions/3716799/partial-specialization- ऑफ-फ़ंक्शन-टेम्पलेट्स – Nim
['std :: inner_product'] (http://en.cppreference.com/w/cpp/algorithm/inner_product) का उपयोग क्यों न करें? लूप को आपके लिए इतना महत्वपूर्ण रूप से रेखांकित कर रहा है? – gwiazdorrr
@Nim लिंक के लिए धन्यवाद - इससे मदद मिली। – htzfun