मुझे टेम्पलेट कक्षाओं के लिए कुछ ऑपरेटर ओवरलोड को परिभाषित करने में कुछ समस्याएं आ रही हैं। चलिए उदाहरण के लिए इस hypothetical कक्षा लेते हैं।कक्षा टेम्पलेट्स पर ऑपरेटर ओवरलोडिंग
template <class T>
class MyClass {
// ...
};
ऑपरेटर + =
इस संकलक त्रुटि में// In MyClass.h MyClass<T>& operator+=(const MyClass<T>& classObj); // In MyClass.cpp template <class T> MyClass<T>& MyClass<T>::operator+=(const MyClass<T>& classObj) { // ... return *this; }
परिणाम:
no match for 'operator+=' in 'classObj2 += classObj1'
ऑपरेटर < <
इस संकलक चेतावनी में// In MyClass.h friend std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj); // In MyClass.cpp template <class T> std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj) { // ... return out; }
परिणाम:
friend declaration 'std::ostream& operator<<(std::ostream&, const MyClass<T>&)' declares a non-template function
क्या मैं गलत यहाँ कर रहा हूँ?
आप कुछ वास्तविक कोड है जो संकलित करने के लिए विफल रहता है पोस्ट कर सकते हैं? – Naveen
@ नवीन: आप एक ज़िप संस्करण प्राप्त कर सकते हैं http://www.box.net/shared/v23rj2f8e7 – Pieter