2012-03-01 64 views
3

我試圖端口/構建G ++到我的系統上運行,並且運行到以下錯誤,而建設的libstdC++:將大括號初始化列表轉換爲類型的錯誤含義?

.../gcc-4.6.2/i686-pc-linux-gnu/libstdc++-v3/include/mutex:226:50: error: could not convert '{0}' from ' <brace-enclosed initializer list> ' to ' std::timed_mutex::__native_type {aka pthread_mutex_t} '

include/mutex相關的代碼是:

class timed_mutex 
{ 
    // ... 
    __native_type _M_mutex; 
    // ... 
    timed_mutex() : _M_mutex(__GTHREAD_MUTEX_INIT) { } // Line 226 
    // ... 
} 

__native_typepthread_mutex_t__GTHREAD_MUTEX_INIT擴展爲{0}

我對C++並不是很熟悉,只是C,但我在這裏看不到任何明顯的錯誤。錯誤是什麼意思?

回答

2

正確的語法是:

timed_mutex() : _M_mutex({__GTHREAD_MUTEX_INIT}) { } // i.e. _M_mutex({{0}}) 

但是該功能只與C++ 11可用。 Demo
對於較老的編譯器,不能使用具有構造函數的初始化程序列表

有2 {}的原因是,pthread_mutex_tunion定義爲shown here。其中包含一個struct,char[24], long int;因此初始化語法自然會有所不同。

更新: 當我試圖編譯<mutex>頭在測試文件,它提供瞭如下錯誤:

/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

很可能是特定的文件如下C++ 11的初始化語法

+0

此代碼是libstdC++的一部分,而不是我寫的代碼。實際上,我只是通過將定義從「{0}」更改爲「{{{0}}}」來實現它,但我有點困惑爲什麼大括號級別很重要。這是C++與C不同的地方嗎? – 2012-03-01 03:39:02

+0

@R,我已經更新了答案。請注意,'pthread_mutex_t'是一個包含3個元素的'union'。可能你的編譯器比較老,或者不支持一些g ++擴展。 – iammilind 2012-03-01 03:45:20

+0

你假設'pthread_mutex_t'的特定實現;我正在向一個不同的系統移植。 – 2012-03-01 04:14:49