初始化引用时出错

时间:2016-07-26 06:11:40

标签: c++ list reference

我正在尝试声明对int列表的引用。当我编译时,我收到expected '(' for function-style cast or type construction的{​​{1}}错误,我在其中声明了一个引用。这是什么问题?我看了一遍,这似乎是如何初始化引用。因为它不会编译,所以我一定会错过任何东西。

list<int>& current;

1 个答案:

答案 0 :(得分:1)

这不是你初始化参考的方式。使用:

list<int>& current = <variable>; // list;

list是变量名称的不良选择。

  1. 使用

    list<int> list;
    

    将导致编译器错误,因为您有:

    using namespace std;
    
  2. 即使您没有使用

  3. list也会是一个糟糕的变量名称

    using namespace std;
    
  4. 我建议删除using namespace std;行并为变量使用不同的名称。

    std::list<int> my_list;
    std::list<int>& current = my_list;
    
相关问题