2013-05-23 16 views
9

मैं इस कोड के लिए एक संकलक त्रुटि मिलती है: इस प्रकारखाका एक shared_ptr तर्क

#include <boost/shared_ptr.hpp> 
#include <boost/make_shared.hpp> 

struct Name 
{ 
}; 
typedef boost::shared_ptr<Name> NamePtr; 

struct Foo 
{ 
    NamePtr name; 
}; 
typedef boost::shared_ptr<Foo> FooPtr; 

template<class T> 
void setName(T item, NamePtr name = boost::make_shared<Name>()) 
{ 
    item->name = name; 
} 

int main() 
{ 
    FooPtr foo = boost::make_shared<Foo>(); 
    setName(foo); 
    return 0; 
} 

के रूप में:

main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&,A2 &&,A3 &&,A4 &&,A5 &&,A6 &&,A7 &&,A8 &&,A9 &&)' : expects 9 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(590) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&,A2 &&,A3 &&,A4 &&,A5 &&,A6 &&,A7 &&,A8 &&)' : expects 8 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(534) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&,A2 &&,A3 &&,A4 &&,A5 &&,A6 &&,A7 &&)' : expects 7 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(480) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&,A2 &&,A3 &&,A4 &&,A5 &&,A6 &&)' : expects 6 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(428) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&,A2 &&,A3 &&,A4 &&,A5 &&)' : expects 5 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(378) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&,A2 &&,A3 &&,A4 &&)' : expects 4 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(330) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&,A2 &&,A3 &&)' : expects 3 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(284) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&,A2 &&)' : expects 2 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(240) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2780: 'boost::shared_ptr<X> boost::make_shared(A1 &&)' : expects 1 arguments - 0 provided 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(198) : see declaration of 'boost::make_shared' 
1>main.cpp: error C2783: 'boost::shared_ptr<X> boost::make_shared(void)' : could not deduce template argument for 'T' 
1>   c:\users\ebargri\desktop\boost_1_49_0\boost\smart_ptr\make_shared.hpp(121) : see declaration of 'boost::make_shared' 

क्या मैं गलत कर रहा हूँ?

अगर मैं इस के साथ टेम्प्लेटेड समारोह की जगह, कोड ठीक संकलित:

void setName(FooPtr item, NamePtr name = boost::make_shared<Name>()) 
{ 
    item->name = name; 
} 

यह भी अगर मैं इसे इस के साथ बदलें संकलन होगा:

template<class T> 
void setName(T item) 
{ 
} 

और यह भी अगर मैं संकलित कर देगा दूसरे तर्क में निम्नानुसार पास करें:

FooPtr foo = boost::make_shared<Foo>(); 
NamePtr name = boost::make_shared<Name>(); 
setName(foo, name); 

यह संकलित नहीं करता है:

setName<FooPtr>(foo); 

अद्यतन:

#include <boost/shared_ptr.hpp> 
#include <boost/make_shared.hpp> 

template<class T> 
void f(T item, boost::shared_ptr<int> name = boost::make_shared<int>()) 
{ 
} 

int main() 
{ 
    f(0); 
} 
+0

आपकी कक्षा में सार्वजनिक निर्माता नहीं होना चाहिए? – stijn

+0

@stijn structs डिफ़ॉल्ट सार्वजनिक रचनाकारों के साथ कक्षाएं हैं – Baz

+0

मुझे पता है कि, अभी तक बढ़ावा नहीं लगता है कि नाम यहां कैसे बनाया जाए? या मैं पूरी तरह से त्रुटि संदेशों को गलत व्याख्या कर रहा हूं .. – stijn

उत्तर

3

यह एक वी.एस. बग है:

यहाँ एक और उदाहरण है। डेमो:

namespace foo { 
    template <typename T> class A {}; 
    template<typename T> A<T> mk_A() { return A<T>(); } 
} 

template<class T> 
void f(T item, foo::A<int> x = foo::mk_A<int>()) { } // triggers the bug 

using foo::mk_A; 
template<class T> 
void g(T item, foo::A<int> x = mk_A<int>()) { } // does not trigger the bug 


int main() { 
    f(0); g(0); 
} 
संबंधित मुद्दे