नीचे दिए गए नमूना कोड में, यह दिखाता है कि boost :: tuple को पहले टेम्पलेट तर्क से पूरी तरह से बनाया जा सकता है। इसके कारण मैं <<
ऑपरेटर लिखने में सक्षम नहीं हूं क्योंकि यह संदिग्ध हो जाता है।बढ़ावा देने के लिए `<<` ऑपरेटर कैसे लिखें :: tuple?
मुझे यह भी समझ में नहीं आता कि ostringstream& << float
भी संदिग्ध है। इसका कोई अंतर्निहित निर्माण नहीं है। यह अस्पष्ट त्रुटि क्यों देता है?
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <sstream>
#include <string>
using namespace std;
class Myclass
{
};
typedef boost::tuple<int,float,Myclass> Mytuple;
ostringstream& operator<<(ostringstream& os_, Mytuple tuple_)
{
float f = tuple_.get<1>();
//os_ << (int)tuple_.get<0>(); // Error because int is implicitly converted into Mytuple. WHYY?
//os_ << tuple_.get<1>(); // No Clue Why this is ambiguous.
//os_ << tuple_.get<2>(); // Error because no matching operator. Fine.
return os_;
}
int main()
{
Mytuple t1;
t1 = 3; // Working because int is implicitly converted into Mytuple!! WHY?
//t1 = 3.0f; // Error because no matching constructor. Fine.
return 0;
}
त्रुटि Mesasge:
tupleTest2.C:18: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
'boost :: tuple' में पहले से ही एक ऑपरेटर <<' है। मैं नहीं देखता कि यह आपके द्वारा प्राप्त त्रुटियों की ओर जाता है लेकिन यह संबंधित हो सकता है। –
इसके अलावा 'boost :: tuple' में tuple तत्वों के 0..n के लिए निर्माता हैं, इसलिए आपके पास एक कन्स्ट्रक्टर' माईटप्ले (int) 'है जो इसे' int' से परिवर्तनीय बनाता है। –
यह मेरे लिए ठीक संकलित करता है - यह जीसीसी 4.5 और जीसीसी 4.7 प्रयोगात्मक के साथ होना चाहिए। आप किस कंपाइलर संस्करण का उपयोग कर रहे हैं? – rodrigo