gpt4 book ai didi

c++ - 当从 GCC 3.1.2 移植到 4.2.2 时编码中断

转载 作者:行者123 更新时间:2023-11-28 01:09:22 25 4
gpt4 key购买 nike

    // Standard Template Library example
#include <iostream>
#include <list>
using namespace std;
// Simple example uses type int
main()
{
list<int> L;
L.push_back(0); // Insert a new element at the end
L.push_front(0); // Insert a new element at the beginning
L.insert(++L.begin(),2); // Insert "</span><span title="Convert this amount" class="currency_converter_link">2</span><span class="currency_converter_text">" before position of first argument
// (Place before second argument)
L.push_back(5);
L.push_back(6);

static list<int>::iterator itr = 0 ;

if(itr == L.end())
{
cout <<"itr is equal to NULL;)" ;
}
else
{
cout <<"Not NULL ";
}


for(itr=L.begin(); itr != L.end(); ++itr)
cout << *itr << " ";

cout << endl;
return 0;
}

我的代码非常简单,但会抛出一个简单的错误。

iterator.cpp:16:29: error: conversion from 'int' to non-scalar type 'std::list<i
nt>::iterator' requested

我对 C++ 很陌生,处理这个错误的最佳方法是什么,以便代码可以很好地编译。仅供引用,当您将相同的代码从 GCC 3.1.2 切换到 4.2.2 时会出现移植问题。基本上整个问题都是围绕着一条线编织的

static list<int>::iterator itr = 0 ;

好像有人指导我阅读有关这些迭代器的好文档。由于旧的 GCC 正在接受这些东西或只是发出警告,但现在他们正在考虑这个和错误。我很好奇,当我们将 NUL 分配给指针并与 NULL 进行比较时,对于迭代器而言,什么是等价的。

请帮忙。谢谢...

最佳答案

修改如下:

   static list<int>::iterator itr = 0 ; 

   static list<int>::iterator itr = L.begin() ; 

顺便说一句,为什么将其声明为静态局部变量?

关于c++ - 当从 GCC 3.1.2 移植到 4.2.2 时编码中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4242327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com