Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 大括号内初始值设定项列表中的左值引用初始化无法编译_C++_Reference_Lvalue - Fatal编程技术网

C++ 大括号内初始值设定项列表中的左值引用初始化无法编译

C++ 大括号内初始值设定项列表中的左值引用初始化无法编译,c++,reference,lvalue,C++,Reference,Lvalue,我有一个非常简单的程序: #include <iostream> using namespace std; int main() { string var {"test"}; string &lr {var}; cout << var << "\n"; cout << lr << "\n"; } 因此,行string&r1{var}完全对应于我的字符串&lr{v

我有一个非常简单的程序:

  #include <iostream>

  using namespace std;

  int main()
  {
     string var {"test"};
     string &lr {var};

     cout << var << "\n";
     cout << lr << "\n";
  }

因此,行
string&r1{var}
完全对应于我的
字符串&lr{var},并且源
字符串
以完全相同的方式初始化。

似乎是编译器的问题。gcc 4.6.3相当陈旧;请注意,它仍然必须使用
c++0x
而不是
c++11
。对于较新的版本(特别是4.9.2),代码很好。

它与本书中的原始示例有何不同?gcc 4.6非常古老;注意,它仍然有
c++0x
而不是
c++11
。您可能想试用更新的版本。@user207933,我已经更新了我的问题。@Angew,好的,我仍然使用Linux Mint 13 LTS提供的,仍然没有时间升级。我会设法测试新版本的,谢谢。
g++ ./test.cpp -g3 -std=c++0x
./test.cpp: In function ‘int main()’:
./test.cpp:9:19: error: invalid initialization of non-const reference of type ‘std::string& {aka std::basic_string<char>&}’ from an rvalue of type ‘<brace-enclosed initializer list>’
string var {"Cambridge"};
string f();

string& r1 {var};         // lvalue reference, bind r1 to var (an lvalue)
string& r2 {f()};         // lvalue reference, error : f() is an rvalue
string& r3 {"Princeton"}; // lvalue reference, error : cannot bind to temporary