错误:在此范围内未声明slist

时间:2015-06-13 09:23:43

标签: c++ list c++14

此代码对我不起作用,我收到了下面提到的编译错误。 有人请帮我指出我的错误。

In function 'int main()':
4:3: error: 'slist' was not declared in this scope
4:9: error: expected primary-expression before 'int'
5:3: error: 'L' was not declared in this scope
12:9: error: expected primary-expression before 'int'
13:3: error: 'back' was not declared in this scope

代码:

int main() {
  slist<int> L;
  L.push_front(0);
  L.push_front(1);
  L.insert_after(L.begin(), 2);
  copy(L.begin(), L.end(),        // The output is 1 2 0
       ostream_iterator<int>(cout, " "));
  cout << endl;

  slist<int>::iterator back = L.previous(L.end());
  back = L.insert_after(back, 3); 
  back = L.insert_after(back, 4);
  back = L.insert_after(back, 5);
  copy(L.begin(), L.end(),        // The output is 1 2 0 3 4 5
       ostream_iterator<int>(cout, " "));
  cout << endl;
}

1 个答案:

答案 0 :(得分:1)

所有问题归结为以下声明

slist<int> L;

抛出错误

slist未在此范围内声明

您需要确保在您的范围内声明slistinclude必要的标题。