2016-05-02 6 views
8

मैं संकलित करने के लिए एक टेम्पलेट श्रेणी के अंदर एक दोस्त को काम करने की कोशिश कर रहा हूं, लेकिन त्रुटि संदेश और चेतावनी मुझे समझ में नहीं आती है। मैंने इस मुद्दे का प्रदर्शन किया है। ...टेम्पलेटेड क्लास फ्रेंड ऑपरेटर सदस्य फंक्शन

template<typename A, typename B> 
C<A, B> operator+<A, B>(const B& lhs, const C<A, B>& rhs); 

क्योंकि operator+ और ( के बीच <A,B> की, मैं वास्तव में आप क्या पता नहीं है गलत है: त्रुटि मैं हो रही है:

prog.cpp:8:57: error: non-class, non-variable partial specialization C operator+(const B& lhs, const C& rhs);

prog.cpp:15:59: warning: friend declaration 'C operator+(const B&, const C&)' declares a non-template function [-Wnon-template-friend] friend C operator+(const B& lhs, const C& rhs);

prog.cpp:15:59: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

#include <iostream> 
using namespace std; 

template<typename A, typename B> 
class C; 

template<typename A, typename B> 
C<A, B> operator+<A, B>(const B& lhs, const C<A, B>& rhs); 

template<typename A, typename B> 
struct C 
{ 
    A val_; 
    C operator+(const C& other) const; 
    friend C<A, B> operator+(const B& lhs, const C<A, B>& rhs); 
}; 

template<typename A, typename B> 
C<A, B> C<A, B>::operator+(const C<A, B>& other) const 
{ 
    C<A, B> c; 
    c.val_ = this->val_ + other.val_; 
    return c; 
} 

template<typename A, typename B> 
C<A, B> operator+(const B& lhs, const C<A, B>& rhs) 
{ 
    C<A, B> c; 
    c.val_ = lhs + rhs.val_; 
    return c; 
} 

int main() 
{ 
    C<string, char> c0,c1; 
    c0.val_ = " C0 "; 
    c1.val_ = " C1 "; 
    cout << "Stuct:" << (c0 + c1).val_ << '\n'; 
    cout << "Friend:" << ('~' + c1).val_ << endl; 
    return 0; 
} 
+0

आप 'ऑपरेटर के पहले घोषणा की जरूरत नहीं है +', गलत है। – Holt

+0

यह समस्या का एक उदाहरण है। मुझे अपने वास्तविक कोड में तीन रूपों की आवश्यकता है: सी ऑपरेटर + (कॉन्स सी और अन्य), सी ऑपरेटर + (कॉन्स बी और अन्य), और सी ऑपरेटर (कॉन्स बी एंड एलएचएस, कॉन्स सी एंड आरएस)। N.B. वास्तविक संकलन कोड नहीं, केवल आवश्यकता के सामान्य रूपों। – JadziaMD

उत्तर

3

इस घोषणा यहाँ करना चाहता था। यदि आप एक टेम्पलेट operator+ विशेषज्ञ हैं, तो आप इस फॉर्म का उपयोग करेंगे, लेकिन आप यहां नहीं हैं, आप ओवरलोडिंग कर रहे हैं।

यह घोषणा किया जाना चाहिए:

template<typename A, typename B> 
C<A, B> operator+ (const B& lhs, const C<A, B>& rhs); 

तो फिर तुम पर स्पष्ट रूप से अपने friend घोषणा है कि आप writting द्वारा एक विशेष संस्करण चाहते में निर्दिष्ट करना चाहिए:

friend C<A,B> operator+<>(const B& lhs, const C<A,B>& rhs); 

आप अपने operator+ से पहले इस डाल करने के लिए की जरूरत है, अन्य कंपाइलर को लगता है कि यह एक गैर-टेम्पलेटेड फ़ंक्शन का विशेषज्ञता है।

वैसे भी, अगर आपके पास C कक्षा के बाहर अपना कोड डालने का कोई वास्तविक कारण नहीं है, तो मैं @ जारोड 42 समाधान पर जाऊंगा।


आपका पूरा कोड इस तरह दिखना चाहिए:

// Declaration of struct C with delayed definition 
template <typename A, typename B> 
struct C; 

// Initial declaration of templated operator+ 
template <typename A, typename B> 
C<A, B> operator+ (const B&, const C<A, B>&); 

// Definition of C 
template <typename A, typename B> 
struct C { 

    friend C operator+<> (const B&, const C&); 

    // This must be AFTER the templated operator+ 
    C operator+ (const C&) const; 
}; 

template<typename A, typename B> 
C<A, B> C<A, B>::operator+(const C<A, B>& other) const { 

} 

template<typename A, typename B> 
C<A, B> operator+(const B& lhs, const C<A, B>& rhs) { 

} 
+0

मेरे पास ऐसा कोई कारण है। :) मैंने उदाहरण में सुझाए गए कोड में बदलाव किए हैं, लेकिन अब मुझे त्रुटि मिलती है: prog.cpp: 19: 24: त्रुटि: 'ऑपरेटर +' की घोषणा गैर-फ़ंक्शन मित्र सी ऑपरेटर + <> (कॉन्स बी एंड एलएचएस, कॉन्स सी & rhs); ^ prog.cpp: 19: 24: त्रुटि: अपेक्षित ';' सदस्य घोषणा प्रोग के अंत में।सीपीपी: 1 9: 26: त्रुटि: '<' टोकन दोस्त सी ऑपरेटर + <> (कॉन्स बी और एलएचएस, कॉन्स सी और आरएएस) – JadziaMD

+1

@JadziaMD से पहले अपेक्षित अयोग्यता-आईडी आपको 'सी ऑपरेटर + <> (कॉन्स बी एंड एलएचएस, कॉन्स सी और आरएएस) ** ** ** ** किसी अन्य गैर-टेम्पलेटेड 'ऑपरेटर +' के अंदर 'सी'। मैं अपना जवाब अपडेट करूंगा। – Holt

5

सरल वर्ग के अंदर कोड इनलाइन के लिए है:

template <typename A, typename B> 
struct C 
{ 
    A val_; 
    C operator+(const C& other) const 
    { 
     C c; 
     c.val_ = this->val_ + other.val_; 
     return c; 
    } 

    friend C operator+ (const B& lhs, const C& rhs) 
    { 
     C c; 
     c.val_ = lhs + rhs.val_; 
     return c; 
    } 
}; 

Demo

कोड में inlined नहीं कक्षा, जिसे घोषणापत्र के आगे घोषित आदेश के रूप में बहुत ध्यान देने की आवश्यकता है, अजीब वाक्य रचना <>:

template <typename A, typename B> struct C; 

template <typename A, typename B> 
C<A, B> operator+ (const B& lhs, const C<A, B>& rhs); 

template <typename A, typename B> 
struct C 
{ 
    A val_; 

    friend C<A, B> operator+<> (const B& lhs, const C<A, B>& rhs); 

    C operator+(const C& other) const; 
}; 


template <typename A, typename B> 
C<A, B> operator+ (const B& lhs, const C<A, B>& rhs) 
{ 
    C<A, B> c; 
    c.val_ = lhs + rhs.val_; 
    return c; 
} 

template <typename A, typename B> 
C<A, B> C::operator+(const C<A, B>& other) const 
{ 
    C<A, B> c; 
    c.val_ = this->val_ + other.val_; 
    return c; 
} 

Demo

संबंधित मुद्दे