2012-03-01 65 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