【发布时间】:2011-12-02 05:27:09
【问题描述】:
我已经从主干版 GCC-4.7.0 构建了 MinGW:http://code.google.com/p/mingw-builds/downloads/list
在这个版本的变化描述中说实现了非静态数据成员初始化器:http://gcc.gnu.org/gcc-4.7/changes.html
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2008/n2756.htm
当我尝试编译这样一个例子时:
#include <iostream>
#include <string>
struct type {
type()
:i(33)
{}
int i;
std::string s("string");
};
int main() {
type t;
std::cout << t.i << " : " << t.s << std::endl;
}
我得到了很多错误,最后是这个:
main.cpp:16:35: 注意:'std::string (type::)(int) {aka std::basic_string (type::)(int)}' 不是从 'const 派生的 std::basic_string<_chart _traits _alloc>' main.cpp:16:35: 注意: 无法从重载函数 't.type::s' 解析地址
但是根据文档,代码是正确的。
【问题讨论】:
-
我没有安装 GCC 4.7 来测试它,而且我的版本不支持成员初始化,但是你用 -std=c++0x 标志编译它吗?
-
请注意,成员初始化器是 GCC 4.7 的新功能,因此它应该是一个简单的编译器错误。