【问题标题】:For loop did not run [closed]For循环没有运行[关闭]
【发布时间】:2020-12-14 23:39:38
【问题描述】:

我得到了我用结构实现类的代码。几乎所有的运算符都被重载了,它们都很好。但是在方法insert() 中是一种奇怪的行为:即使是最简单的方法,它也没有运行for loop。如果它在ArrayList 上产生不同的for-each loop 效果很好。

class ArrayList
{
public:
   int siz;
   T *ptr;
   int cap;

   struct Iterator
   {
       T *sptr;
       bool operator<=(const Iterator &lhs);

       Iterator &operator++();

       Iterator &operator--();
       T &operator*();
   };
   Iterator &operator++();
   Iterator &operator--();
};

typename ArrayList<T>::Iterator ArrayList<T>::insert(Iterator it, T &&x)
{
   for (int a = 0; a++; a <= 5)
   {
       cout << "ins" << endl;
   }
   cout << "ah";
}


template <class T>
typename ArrayList<T>::Iterator &ArrayList<T>::Iterator::operator++()
{
   Iterator::sptr++;
   return *this;
}

当我使用 '''insert()''' 方法时,它只打印“ah”。 -Wall 没有发现任何问题。

我真的很困惑

【问题讨论】:

  • for (int a = 0; a &lt;= 5; a++) 老兄!
  • 只是挑剔一点:显示的代码不是minimal reproducible example。它包含的代码比展示您的问题所需的代码多。同时它也缺少构建它的代码(insert 函数声明在哪里)。

标签: c++ loops for-loop


【解决方案1】:

您似乎弄乱了语句的顺序。 #2 是条件,所以它应该是这样的:

(int a = 0; a <= 5; a++) 

不是这样的

(int a = 0; a++; a <= 5)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多