c++ - 非静态数据成员初始化器问题

标签 c++ c++11

我从主干版 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: note: 'std::string (type::)(int) {aka std::basic_string (type::)(int)}' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>' main.cpp:16:35: note: could not resolve address from overloaded function 't.type::s'

但是根据文档,代码是正确的。

最佳答案

问题似乎是在确定您是声明函数还是对象以及编译器正在选择函数时模棱两可。

您应该尝试使用以下语法初始化字符串:

std::string s = "string";

如果我们点击 GCC 发行说明中有关非静态数据成员初始化程序 (proposal N2756) 的链接,他们会在问题 1 中提到这一点,并附上解决说明:

CWG had a 6-to-3 straw poll in Kona in favor of class-scope lookup; and that is what this paper proposes, with initializers for non-static data members limited to the “= initializer-clause” and “{ initializer-list }” forms.

关于c++ - 非静态数据成员初始化器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7674357/

相关文章:

c - 设计实体系统时如何避免 C 中的虚函数

类中 vector 的 C++ vector

c++ - 在 libc++ 的内联命名空间中转发声明类的可移植方法是什么?

visual-studio-2010 - 播种 default_random_engine?

c++ - 带有第二个参数=0 的 eventfd_write() 不起作用?

c++ - 编译器提供语法错误,Visual Studio中未显示。我能做什么?

qt - 通过 lambda 连接 QPushButton

c++ - 强制值为 boolean 值 : (bool) makes warning, !!没有

java - 计算机视觉、C++ 或 Java

c++ - move 语义和 std::future