How do you put the dynamic body into a class method?



  • Class:

    class Snumbers{
       double metod(double m[]) { 
       /*
       ---code---
       */ 
       return m[0]; // для примера
       }
    };
    

    Major Programme:

    int main(){
       Snumbers A;
       int num; 
    

    cin >> num;
    double *m1 = new double[num];

    for(int i = 0; i < num; i++){
    cin >> m1[i];
    }
    cout << A.metod(m1);
    }

    The question is, how am I supposed to transfer the mass to that class method so I could work there with him, and that method returned what I'd write?

    Updating

    I forgot to add public in class before method. That's how it should be.

     class Snumbers{
    public:
    double metod(double m[]) {
    /*
    ---code---
    */
    return m[0]; // для примера
    }
    };

    I'm making a mistake:

    [bcc32 Error] F1.cpp(27): E2247 'Snumbers:metod(double *)' is not accessible



  • You should also transfer the size of the mass to the class method, or how can you work with it if you don't know its size?

    double metod( double * arr, size_t size ) {
        ...
    }
    

    ...

    cout << A.metod( m1, num );




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2