2011-05-13 19 views
6

मैं निम्नलिखित कोड के साथ समस्या है "गैर वर्ग प्रकार के रूप में पहले से ही एक वर्ग प्रकार के रूप में घोषित किया गया":फॉरवर्ड-घोषित प्रकार और

template <typename T> 
    void foo(struct bar & b); 
    struct bar {}; 
    int main(){} 

यह जीसीसी पर सफलतापूर्वक संकलित है, लेकिन साथ MSVC (2008) पर विफल रहता है निम्न त्रुटि:

C2990: 'bar' : non-class type as already been declared as a class type

कोड गलत है या यह MSVC में एक बग है?

यह काम करता है अगर मैं टेम्पलेट परिभाषा से पहले struct bar; जोड़ता हूं।

+1

इसके अलावा कुलपति ++ 2010 और परमेश्वर के साथ विफल रहता है, क्या एक भयानक सी ++ विकास वीएस के लिए आईडीई बन गया है! –

उत्तर

3

और हम अपने विजेता है:

https://connect.microsoft.com/VisualStudio/feedback/details/668430/forward-declared-type-and-non-class-type-as-already-been-declared-as-a-class-type

Thank you for reporting this issue. This is indeed a case of non-conformant behaviour in VC++. However, a simple workaround is to reorder the declarations so that the declaration "struct bar" is known when the template declaration is encountered. Due to the low severity of this bug and our priorities, we regret that we cannot fix the bug in the next release of the compiler but we will consider it for a future release.

Regards,

Tanveer Gani Visual C++ Team

0

आपके पास संभवतः struct bar {}; कोड के इस ब्लॉक से ऊपर है (संभवतः एक हेडर फ़ाइल में)। देखें http://msdn.microsoft.com/en-us/library/zfcw8kk9.aspx

संपादित करें:

C2990 can also occur due to a breaking change in the Visual C++ compiler for Visual C++ 2005; the compiler now requires that multiple declarations for the same type be identical with respect to template specification.

foo के बाद से टेम्प्लेटेड है और bar "आगे की घोषणा की," किया जा रहा है foo तर्क सूची में, यदि आप struct bar {}; ऊपर foo लिए कदम क्या होता है: ऊपर के लिंक से भी ?

+0

नहीं, यह पूर्ण, न्यूनतम कोड है। –

+0

@Hello आपकी टिप्पणी पर आधारित आधार। –

+0

आपके संपादन का जवाब: हाँ, यह तब काम करेगा। इसके अलावा, अगर मैं स्ट्रक्चर बार जोड़ता हूं तो यह काम करता है; टेम्पलेट परिभाषा से पहले। जैसे मैंने अपने प्रश्न में लिखा है। –

3

ज्यादातर स्थितियों में, एक सी (या सी ++ कंपाइलर) आपके स्रोत कोड पर सख्ती से ऊपर से नीचे काम करता है। struct bar को संदर्भित करने का प्रयास करने से पहले आपको forward declaration की आवश्यकता है, अन्यथा संकलक को यह पता नहीं चलेगा कि यह मौजूद है।

+0

आप इनलाइन आगे की घोषणा कर सकते हैं। 'शून्य फू (स्ट्रक्चर बार & b); 'की तरह। ** स्ट्रक्चर ** कीवर्ड यहां आगे की घोषणा है। –

+0

@ हैलो: वास्तव में? मैं यह नहीं कह रहा हूं कि आप गलत हैं, केवल मैंने इसे पहले कभी नहीं देखा है। क्या आप एक संदर्भ प्रदान करें? –

+0

@ ओली: टेम्पलेट के बिना इसे आज़माएं, यह संकलित होगा। :) – Xeo

0

यह मान्य कोड की तरह दिखता है। जो भी एमएसवीसी कर रहा है, यह कुछ अजीब गैर-अनुरूप व्यवहार प्रतीत होता है, जो मैं देख सकता हूं।

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