# Class Syntax Help

• 08-31-2005
RpgActioN
Class Syntax Help
I'm doing exercises out of a book for self-learning purposes, and one of the exercises is:

Design, implement and test class Average. An object of type Average should accept values that are put into it. At any point in time it should be possible to retrieve their cumulative arithmetic average.

Well, I think (or, at least, hope) that my math is right in the sense that this is what I would have to do in order to complete the calculations, but my compiler (Dev-C++) is giving me a ton of errors saying "invalid types double[int] for array subscript". I know this is referring to the loops where I have numbers[x], but it just gives me the error "invalid types double[double] for array subscript" when I try changing x to the type double. I really have no clue as to how to get around this:

Code:

```#include <iostream> using std::cout; using std::cin; class Average { public:         double numbers;         int x;         int numsmone;         Average        (int nums)         {                 numsmone = nums - 1;                                for (x = 0; x < nums; ++x)                 {                         if (x == 0)                                 cout << "Enter the 1st number: ";                         else if (x == 1)                                 cout << "Enter the 2nd number: ";                         else if (x == 2)                                 cout << "Enter the 3rd number: ";                         else                                 cout << "Enter the " << x << "th number: ";                         cin >> numbers[x];                 }         }         double CalculateMean ()         {                 double mean;                 for (x = 0; x < numsmone; ++x)                         mean = numbers[x] + numbers[x+1];                 mean = mean / (numsmone + 1)                 return mean;         } }; int main () {         int nums;         cout << "Enter the number of numbers you wish to average: ";         cin >> nums;         Average Avgone(nums);         cout << "The mean is " << Avgone::CalculateMean;         cin.get();         return 0; }```
• 09-01-2005
major_small
Code:

```#include <iostream> using std::cout; using std::cin; class Average {         //these should be private data members         private:                 //for an array, you must either specify a size or use a                 //pointer and dynamically allocate memory.  I used a pointer:                 double*numbers;                 int x;                 int numsmone;         public:                 //functions should only be defined in the class body.                 //your implementations should be somewhere else.                 Average(int nums);                 //this is a destrctor.  we need it in this case because we                 //dynamically allocated memory that needs to be freed up.                 //when the class runs out of scope, this is automatically called                 ~Average();                 double calculateMean(); }; //here's the implementation for the constructor.  Usually constructors only //initialize the class.  putting this much functionality into your constructor //should probably be avoided. Average::Average(int nums) {         numsmone = nums - 1;                //here I'm dynamically allocating the memory for the array         numbers=new double[nums];         for (x = 0; x < nums; ++x)         {                 if (x == 0)                         cout << "Enter the 1st number: ";                 else if (x == 1)                         cout << "Enter the 2nd number: ";                 else if (x == 2)                         cout << "Enter the 3rd number: ";                 else                         cout << "Enter the " << x << "th number: ";                 cin >> numbers[x];         } } //this is the destructor.  don't worry about calling it. Average::~Average() {         //this deallocates the memory from before.         delete[]numbers; } double Average::calculateMean() {         double mean;         for (x = 0; x < numsmone; ++x)                 mean = numbers[x] + numbers[x+1];         mean = mean / (numsmone + 1);        //forgot a semicolon         return mean; } int main () {         int nums;         cout << "Enter the number of numbers you wish to average: ";         cin >> nums;         Average Avgone(nums);         //on this next line, you forgot the (), and you're supposed to use the         //dot (.) operator, not the scope resolution operator (::)         cout << "The mean is " << Avgone.calculateMean();         cin.get();         return 0; }```
I fix your logic though (it's wrong)