简体   繁体   English

将数组从C ++构造函数传递给函数

[英]Passing arrays from C++ constructor to function

If I have a class with a constructor containing an array and I want to pass each member of this array to a function of this class for an addition how can I do so? 如果我有一个带有包含数组的构造函数的类,并且想将此数组的每个成员传递给此类的函数以进行添加,该如何做呢?

eg Constructor implementation 例如构造函数的实现

person::person 
{
  double NoPerson[150]; //no of people  
  for(int j=0;j<=150;j++)
  {
    NoPerson[j]=1.00;//has to set to 1 pound
  }

  double person::aveWage(double NoPerson[])
  {
    double total;
    double NoPerson[150];

    using namespace std;

    for(int i=0;i<=150 ;i++)
    { 
      total=total+cashCus[i];
    }
    cout<<"total cash for all customers "<<total <<endl;
}

Does anyone know how this can be achieved? 有人知道如何实现吗?

Does it really have to be an array? 它真的必须是一个数组吗? If so you need to pass the size along with it, eg int add(int[] items, int size) . 如果是这样,则需要将大小一起传递,例如int add(int[] items, int size) This tutorial covers the basics of arrays: http://www.cplusplus.com/doc/tutorial/arrays/ 本教程介绍了数组的基础知识: http : //www.cplusplus.com/doc/tutorial/arrays/

However, arrays are more of a "plain C" thing. 但是,数组更像是“普通C”。 If you are using C++, you're almost always better off using a vector . 如果您使用的是C ++,那么使用vector几乎总是更好。

I don't know if I understand you right, but it seems to me that you have an array ( NoPerson ) that you want to initialize in the constructor, and then use from the other functions in your class. 我不知道我是否理解正确,但是在我看来,您有一个要在构造函数中初始化然后在类中的其他函数中使用的数组( NoPerson )。

In that case you should not make it a local variable, because then the other function in the class can't access it. 在那种情况下,您不应将其设置为局部变量,因为否则类中的其他函数将无法访问它。 Instead you make it a member variable of the class, just like the functions. 而是像函数一样,使它成为类的成员变量。

Something like this: 像这样:

class Person
{
public:
    Person()
        {
            for (int i = 0; i < 150; i++)
                NoPerson[i] = 1;
        }

    double total()
        {
            double total;

            for (int i = 0; i < 150; i++)
                total += NoPerson[i];

            return total;
        }

private:
    // Variables and functions in the 'private' section can only be
    // referenced by the function in this class.

    int NoPerson[150];  // This is a member variable
}

As you can see in the above example, the variable NoPerson is defined in the class and not in the functions . 如上例所示,变量NoPerson是在类中定义的,而不是在函数中定义的 This makes it accessible to all functions in the class. 这使得该类中的所有函数都可以访问它。

your code is kind of broken. 您的代码有点坏。 You have actually three different arrays called 'NoPerson' in the code, one is local to the constructor, one is the argument to aveWage, and then you have a locally declared NoPerson array within aveWage. 实际上,您在代码中有三个不同的数组,称为“ NoPerson”,一个在构造函数中本地,一个是aveWage的参数,然后在aveWage中有一个本地声明的NoPerson数组。 Additionally, you are not using the arrays for anything, and the array in the constructor is deleted when you leave the constructor. 此外,您不使用数组做任何事情,并且当您离开构造函数时,构造函数中的数组将被删除。 If you want to call aveWage from the constructor with NoPerson array as the argument to aveWage, you can do this, but the local declaration in aveWage will mask it. 如果要使用NoPerson数组作为aveWage的参数从构造函数中调用aveWage,则可以执行此操作,但是aveWage中的本地声明将对其进行屏蔽。

1) You got your array bounds wrong (it should be < 150 ). 1)您的数组边界错误(应该< 150 )。

2) In C++98/03, you cannot initialize member arrays; 2)在C ++ 98/03中,不能初始化成员数组; in C++11 you can (though initialzing 150 non-zero values is cumbersome): person::person() : NoPerson{1., 1., 1.} { } (actually 150 elements). 在C ++ 11中,您可以(尽管初始化150个非零值很麻烦): person::person() : NoPerson{1., 1., 1.} { } (实际上是150个元素)。 (It's unlikely that this problem would survive a proper design revision, though.) (不过,这个问题不太可能在适当的设计修订中仍然存在。)

3) the aveWage loop could be written with std::accumulate (eg std::cout << "total = " << std::accumulate(cashCus, cashCus + 150, 0) << std::endl; . Currently your code is broken because you never initialize total . 3) aveWage循环可以用std::accumulate编写(例如std::cout << "total = " << std::accumulate(cashCus, cashCus + 150, 0) << std::endl;目前,您的代码破裂,因为您从未初始化total

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

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