2015-06-26 7 views
7

मैं निम्नलिखित कोड (सरलीकृत), जो जीसीसी में ठीक संकलित, लेकिन वी.एस. में एक त्रुटि देता है:VS2013 - स्थिर स्थिरांक पहले से ही परिभाषित किया है

// main.cpp 
#include "test.h" 
int main() { 
    return 0; 
} 

// test.h 
#pragma once 
class Test { 
    static const int TEST = 3; 
}; 

// test.cpp 
#include "test.h" 
const int Test::TEST; 

त्रुटि:

main.obj : error LNK2005: "private: static int const Test::TEST" ([email protected]@@0HB) already defined in test.obj 

यह एक है वीएस बग या जीसीसी गलत तरीके से मुझे स्टेटिक कॉन्स सदस्य को स्पष्ट रूप से परिभाषित करने की अनुमति दे रहा है?

अद्यतन:C++ Standard (9.4.2.3) में यह पाया:

If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (5.20). A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions. — end note ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.

अद्यतन # 2: एक bug report पाया है, जो दावा है कि यह अगले मुख्य संस्करण में तय हो गई है।

+0

क्या होगा यदि आप अपने 'test.cpp' से' const int test :: test; 'हटाते हैं, तो क्या gcc कोड संकलित करेगा? – ixSci

+0

ऐसा लगता है, लेकिन मैं कोड के साथ काम कर रहा हूं जो स्पष्ट रूप से उन पंक्तियों को परिभाषित करता है और इसे संपादित नहीं करना चाहता क्योंकि यह अगले svn अद्यतन पर फिर से टूट जाएगा। – riv

+0

धन्यवाद, मैं सिर्फ यह जानना चाहता था कि जीसीसी के पास एक अलग तरह का बग है या नहीं। मैंने सोचा कि यह आपको सभी मामलों में स्पष्ट रूप से निरंतर परिभाषित करने की आवश्यकता है। लेकिन अगर ऐसा नहीं होता है तो केवल एमएसवीसी में एक बग है। – ixSci

उत्तर

2

ठीक उसी तरह जैसा आपने कहा था कि यह एक एमएसवीसी bug था। कोड डिफ़ॉल्ट संकुल विकल्प के साथ विजुअल स्टूडियो 2015 आरसी में पूरी तरह से संकलित और चलाता है।

enter image description here

संकलक सोचता है कि "स्थैतिक स्थिरांक पूर्णांक टेस्ट = 3," और "कॉन्स इंट टेस्ट :: टेस्ट;" एक ही चर की दो अलग-अलग परिभाषाएं हैं। इसे ठीक करने के अपने संस्करण में आप .cpp फ़ाइल में स्थिर चर मूल्य को सेट कर सकें:

// test.h 
#pragma once 
class Test { 
    static const int TEST; 
}; 

// test.cpp 
#include "test.h" 
const int Test::TEST = 3; 
1

Microsoft Extensions to C and C++ सक्षम होने पर, संकलक बाहर के वर्ग परिभाषा स्वचालित रूप से उत्पन्न करता है। कुछ कंपाइलर संस्करण शायद छोटी हैं और जब आप इसे मैन्युअल रूप से परिभाषित करते हैं तो भी यह ऑटो-डेफिनिशन करते हैं (उदाहरण के लिए पोर्टेबल कोड लिखते समय)।

आप या तो एक्सटेंशन अक्षम कर सकते हैं, या _MSC_EXTENSIONS मैक्रो के लिए जांच सकते हैं। यह परिभाषित किया गया है कि जब/ज़ी विकल्प सेट किया गया है। उदाहरण के लिए:

#ifndef _MSC_EXTENSIONS 
    const int MyClass::MyStaticMember; 
#endif 
संबंधित मुद्दे