简体   繁体   English

将对象数组传递给成员函数

[英]Pass object array into member function

Here is the class defination: 这是类定义:

class Unit
{
    public:
     Unit();
     ~Unit();
     void set_unit(int a);
     void modify_flag(Unit&);
     void modify_array(Unit array[], int len);  // ?? The problem
     int show_unit();

    private:
     int ai;

};

And the implementation of the member functions: 并执行成员函数:

void Unit::set_unit(int a)
{
     ai = a;
}

void Unit::modify_flag(Unit& u)
{
     u.set_unit(20);
}

void Unit::modify_array(Unit array[], int len)  // ?? The problem
{
     for (int i = 0; i < len; ++i)
     {
        modify_flag(array[i]);
        array[i].modify_array(array, len);
     }
}

int Unit::show_unit()
{
    return ai;
}

And finally the main code: 最后是主要代码:

int main(int argc, char const *argv[])
{
    int len = 10;
    Unit* array = new Unit[len];

    for (int i = 0; i < len; ++i)
    {
        array[i].set_unit(0);
    }

    array[5].modify_array(array,len);  // ?? The problem


    for (int i = 0; i < len; ++i)
    {
        cout << array[i].show_unit() << endl;
    }

    delete [] array;

    return 0;
}

I passed an array of objects into the member function of the class as the parameter, but it aborted suddenly. 我将一个对象数组作为参数传递给类的成员函数,但突然中断。 I have checked my code many times to make sure the counter did not accumulate over the array length. 我已经检查了我的代码很多次,以确保计数器没有在数组长度上累积。 Therefore, I think there must be something wrong with the object array as the parameter, but I could not figure it out. 因此,我认为对象数组作为参数肯定有问题,但我无法弄清楚。 What happened with my code ?? 我的代码发生了什么??

  1. Define the contructor & destructor of Unit Class. 定义单元类的构造函数和析构函数。
  2. To create array of objects : Unit *array[len]. 创建对象数组:Unit * array [len]。
  3. Now you need to instantiate the objects by using new operator. 现在,您需要使用new运算符实例化对象。 Eg. 例如。 array[1] = new Unit(); array [1] = new Unit();
  4. As you are using an object pointer you need to use arrow (->) operator to call functions instead of dot(.) operator. 使用对象指针时,需要使用箭头(->)运算符而不是点(。)运算符来调用函数。
  5. Now when you call member functions from other member function of the same class you don't need to create and object to call that method. 现在,当您从同一类的其他成员函数调用成员函数时,无需创建和对象来调用该方法。
  6. One more point which was mentioned earlier by some other personnel modify_array calls itself and it is an infinite recursion. 其他人员前面提到的另外modify_array称为自身,它是无限递归。

You have uncontrolled recursion. 您具有不受控制的递归。

In modify_array the program will call modify_array ,程序将调用

array[i].modify_array(array, len);

len times, each of which will call len次,每次都会调用

array[i].modify_array(array, len);

len times, each of which will call len次,每次都会调用

array[i].modify_array(array, len);

len times, each of which will call len次,每次都会调用

array[i].modify_array(array, len);

len times... len次...

You should be able to see where this is going. 您应该能够看到前进的方向。

Unfortunately, I'm not sure what your goal is so a can't suggest a proper solution, but You must have some exit condition to stop the chain of calls before you run out of automatic storage (most likely stack space). 不幸的是,我不确定您的目标是什么,因此无法提出适当的解决方案,但是您必须具有一些退出条件才能停止调用链,然后用尽自动存储空间(很可能是堆栈空间)。

For example you could 例如你可以

void Unit::modify_array(Unit array[], int len)  // ?? The problem
{
     for (int i = 0; i < len; ++i)
     {
        modify_flag(array[i]);
        array[i].modify_array(array, len - 1); // note the -1
     }
}

so that every iteration looks at less of the array. 这样,每次迭代都只关注较少的数组。 Eventually len will be 0 and i < 0 will result in no further calls. 最终, len将为0,而i < 0将导致不再进行呼叫。 What good this does you, I can't say, but it stops the recursion. 我不能说这对您有什么好处,但这会阻止递归。

Do you need the recursion at all? 您是否完全需要递归? I don't know. 我不知道。

Thanks user4581301 for help! 感谢user4581301的帮助! I finally found the mistake I made. 我终于找到了我犯的错误。 The code I had written was to modify the whole array with a random starting index, so I tried to do it by resursive calls. 我编写的代码是使用随机的起始索引来修改整个数组,因此我尝试通过递归调用来实现。 I forgot to put the terminal condition, which must be the most important part, in my recursive function. 我忘了在我的递归函数中添加终止条件,该条件必须是最重要的部分。

void Unit::modify_array(Unit array[], int len) 
{
 for (int i = 0; i < len; ++i)
  {
     if(need_to_modify(array[i]))
       array[i].modify_array(array, len);
   }
}

Something like this to jump out the loop. 这样的事情会跳出循环。 This was the my practice to get familiar with the recursive function. 这是我熟悉递归函数的实践。 Thanks everyone. 感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM