मैं संकलित करने के लिए एक टेम्पलेट श्रेणी के अंदर एक दोस्त को काम करने की कोशिश कर रहा हूं, लेकिन त्रुटि संदेश और चेतावनी मुझे समझ में नहीं आती है। मैंने इस मुद्दे का प्रदर्शन किया है। ...टेम्पलेटेड क्लास फ्रेंड ऑपरेटर सदस्य फंक्शन
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;
}
आप 'ऑपरेटर के पहले घोषणा की जरूरत नहीं है +', गलत है। – Holt
यह समस्या का एक उदाहरण है। मुझे अपने वास्तविक कोड में तीन रूपों की आवश्यकता है: सी ऑपरेटर + (कॉन्स सी और अन्य), सी ऑपरेटर + (कॉन्स बी और अन्य), और सी ऑपरेटर (कॉन्स बी एंड एलएचएस, कॉन्स सी एंड आरएस)। N.B. वास्तविक संकलन कोड नहीं, केवल आवश्यकता के सामान्य रूपों। – JadziaMD