1

I'm practicing in order to better understand dynamic arrays and using them in a class. However, I'm struggling to call my functions inside the class. I have no issue with my int size variable, but my int myArray variable is giving me problems. I get the error "expected a member name" when I try to call my void functions in my main function. Are arrays not allowed to be used in this situation?

#include <iostream>

using namespace std;

class myClass
{
public:
    int size;
    int* myArray = new int[size];
    void storeData(int& size, int (&myArray)[]);
    void printData(int& size, int(&myArray)[]);
};

void myClass::storeData(int& size, int(&myArray)[]) 
// Stores array data.
{
    cout << "Enter Size of the array: ";
    cin >> size; 
    // User determines array size.
    for (int x = 0; x < size; x++)
    {
        cout << "Array[" << x << "]: ";
        cin >> myArray[x];
        // User determines array values.
        cout << endl;
    }
}

void myClass::printData(int &size, int(&myArray)[])
// Displays values of the array.
{
    cout << "Value of the arrays are: ";
    for (int x = 0; x < size; x++)
    {
        cout << myArray[x] << "  ";;
    }
    delete[]myArray;
}

int main()
{
    myClass object;
    object.storeData(object.size, object.(&myArray)[]); 
    // E0133 expected a member name.
    object.printData(object.size, object.(&myArray)[]);
    // E0133 expected a member name.
}
  • 1
    Unrelated to the errors you get, but what is the value of `size` when you do `int* myArray = new int[size];`? Think about that for a moment. – Some programmer dude Mar 09 '21 at 18:48
  • 1
    As for your problem, you seem to have some basic misunderstanding on how functions in general works. Perhaps you should take a little more time with [some decent books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and do some more basic exercises first? – Some programmer dude Mar 09 '21 at 18:50
  • 1
    `object.(&myArray)[]` what's that supposed to do? And why do you pass the members of an object to it's own member funtion? The member function can already access the objects data. It seems like you are mostly guessing and expecting the compiler to guess your intentions. A good book could really help you. – Lukas-T Mar 09 '21 at 18:51
  • Well I thought if I didn't pass by reference, then whenever I change the value of my array it would get reset after my void function finishes. – DefinitelyConfused Mar 09 '21 at 19:07
  • @DefinitelyConfused You don't need to pass anything to these functions. Are they supposed to modify the members `size` and `myArray` of an actual instance (like `myClass object;`) or are they supposed to modify external value, that are possibly completely unrelated to the object? In the second case they should be static or no members at all. – Lukas-T Mar 10 '21 at 07:24

1 Answers1

1

There are a couple issues here, I will try to address all of them.

When passing an array to a function, never use [] syntax. In C and C++, arrays decay to pointers, so we do not need [] nor &.

This is valid syntax to pass an array:

int my_array [] = {1,2,3,4};
my_function(my_array, 4);

...

void my_function(int * array, size_t size)
{
  //Iterate over the array or do something...
}

In addition, if a function exists within a class, it can access class members freely, meaning we do not have to pass them in at all. See the following change to your code:

void myClass::storeData(int size) 
// Stores array data. We do NOT need a pointer to the object array, we already have it!
{
    cout << "Enter Size of the array: ";
    cin >> size; 
    // User determines array size.
    for (int x = 0; x < size; x++)
    {
        cout << "Array[" << x << "]: ";
        cin >> myArray[x];
        // User determines array values.
        cout << endl;
    }
}

Lastly, dynamic sized arrays must be allocated dynamically. Do not use int* myArray = new int[size]; in your class definition, because size is not yet initialized. Instead, use a constructor or use your store_data function to allocate the memory.

class myClass
{
public:
  size_t size;
  int * myArray; //Do not allocate anything here...

  myClass(size_t size)
  {
    this->size = size;
    myArray = new int[size];
  }
};

You can get the size however you want, via user input, etc. and pass this to the constructor or an allocator function like storeData.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Lucas Streanga
  • 469
  • 2
  • 7
  • @HolyBlackCat no, it would not... passing my_fuction(myArray[]) would never work. In the function definition, you may use my_function(int array[]) of course, but not when calling it. Notice I specifically said when passing an array to a function, NOT defining a function. – Lucas Streanga Mar 09 '21 at 20:31
  • My bad, thought you were talking about the definition. – HolyBlackCat Mar 09 '21 at 20:33
  • @HolyBlackCat you're good, didn't mean to come off as hostile. I just don't want to confuse the OP because C array syntax and semantics can be confusing for beginners. – Lucas Streanga Mar 09 '21 at 20:37
  • In `void myClass::storeData(int size)` you must omit the parameter `size` because it will shadow `myClass::size`. – Lukas-T Mar 10 '21 at 07:20