2017-10-07 26 views
0

c++當我嘗試編譯下面的代碼,我收到了相互矛盾的聲明錯誤:重新定義UINT鍵入C++

#include <iostream> 
using namespace std; 

typedef uint_least64_t uint; 

int main() { 
    uint i = -1; 
    cout << i << endl; 
} 

錯誤:

main.cpp:5:24: error: conflicting declaration ‘typedef uint_least64_t uint’ 
typedef uint_least64_t uint; 
         ^~~~ 
In file included from /usr/include/stdlib.h:275:0, 
       from /usr/include/c++/6/cstdlib:75, 
       from /usr/include/c++/6/ext/string_conversions.h:41, 
       from /usr/include/c++/6/bits/basic_string.h:5417, 
       from /usr/include/c++/6/string:52, 
       from /usr/include/c++/6/bits/locale_classes.h:40, 
       from /usr/include/c++/6/bits/ios_base.h:41, 
       from /usr/include/c++/6/ios:42, 
       from /usr/include/c++/6/ostream:38, 
       from /usr/include/c++/6/iostream:39, 
       from main.cpp:1: 
/usr/include/x86_64-linux-gnu/sys/types.h:152:22: note: previous declaration as ‘typedef unsigned int uint’ 
typedef unsigned int uint; 
         ^~~~ 

我認爲衝突的聲明錯誤是因爲類型關於uint的聲明已經存在於某種語言中,並且我認爲該類型爲uint_least32_t,因爲:

#include <iostream> 
using namespace std; 

int main() { 
    uint i = -1; 
    cout << i << endl; 
} 

返回(2^32)-1的整數值。因此在c++中有可能將uint重新定義爲uint_least64_t

+0

顯示其餘的代碼。 – melpomene

+0

問題已編輯。請讓我知道是否有任何其他您需要的信息。 – Sajeeb

回答

0

uint不是標準C++的一部分。

您的代碼庫中的某些內容很可能會引入它,或者甚至可能是您的編譯器附帶的C++標準庫。您可能很幸運,圖書館已經引入了std::uint,並且當您編寫污染聲明using namespace std;時,您將其引入全局名稱空間。

不幸的是,一旦它被引入,你就不能取消或者隱藏class

所以你只需要使用不同的符號。

+0

「可悲的是,一旦它被引入,你就不能取消類型定義類型或隱藏類。」我想這意味着我必須將uint的每個實例都更改爲uint_least64_t或 – Sajeeb

+0

的typedef啊,現在你粘貼了更多的代碼,你永遠不會知道,刪除'using namespace std;'可能會修復它。 – Bathsheba

+0

不幸的是,它不 – Sajeeb

2

你可以從錯誤信息看,你的平臺<iostream>拉進<string>,其中拉動<cstdlib>上,拉入<stdlib.h>,拉入<sys/types.h>

查看源代碼,我們可以看到,<sys/types.h>做到這一點:

#ifdef __USE_MISC 
/* Old compatibility names for C types. */ 
typedef unsigned long int ulong; 
typedef unsigned short int ushort; 
typedef unsigned int uint; 
#endif 

#ifdef __USE_MISC後衛是feature_test_macros系統的一部分。如果您要求_DEFAULT_SOURCE(如果您不要求任何其他設置,也會設置此項),內部設置爲__USE_MISC

因此,你應該能夠通過與-ansi或的-std=...選項(例如-std=c++11)一個編譯繞過問題。