Home > OS >  Passing a pointer to an int array to a member function, error: invalid types 'int[int]' fo
Passing a pointer to an int array to a member function, error: invalid types 'int[int]' fo

Time:11-14

Ok, I'm fairly new to programming, and c so please take it easy on me. I am trying to write a program that takes in the dimensions of a metal plate for a 2-D finite element method analysis (thickness neglected). So, I created a class for my part (the plate), the elements for the mesh, and the nodes for the elements. The mesh will consist of square elements and will be applied over the front face of the plate. Right now, I'm working on getting the mesh sorted out before I move on to the element and node classes.

I'm using (or wanting to use) dynamic allocation to create a 2-D array (my mesh) containing the elements of the mesh. I'm trying to write a function, "meshingPart", to create the 2-D array with the number of rows being the height of the plate, and the columns being the length of the plate.

When I run the program, I get these errors and I'm not sure how to fix them:

 In member function 'void PartClass::meshingPart(int&, int, int)':
 error: invalid types 'int[int]' for array subscript

 At global scope:
 error: expected constructor, destructor, or type conversion before '(' token

Also, when I use my printPart() function, will it print the pointer's address, or the values of the array? I'm not completely sure about this, I'm also new to pointers.

Any help would be much appreciated! Thanks in advance.

class PartClass
{
    private:
      const int HEIGHT; // mm
      const int LENGTH; // mm
      const int WIDTH;  // mm
      const int SEED;   // mm
      const int MESHROW; 
      const int MESHCOL;
      int *partMesh; // Mesh array - an int pointer
      
      // Creates the mesh for the part by generating elements to fill the width and length 
      // of the part. The elements are stored in a 2-D array. 
      void meshingPart(const int &partMesh, int inRow, int inCol);
    
    public:
      // Constructs a part with the given parameters, seeds the part for the mesh, 
      // then creates the mesh by generating square elements with length = height = SEED. 
      PartClass(int inHeight, int inLength, int inWidth, int inSeed);

      void printPart()
      {
        cout << "Part mesh:" << *partMesh << endl;
      }
};

class ElementClass
{
    private:
      int elemID;
      static int numElems;

      // Shape functions:
      int N1;
      int N2;
      int N3;
      int N4;
      
    public:

      // Default constructor
      ElementClass()
      {
        elemID = numElems;
        numElems  ;
      };
};

PartClass :: PartClass(inHeight, inLength, inWidth, inSeed)
{
  HEIGHT = inHeight;
  LENGTH = inLength;
  WIDTH = inWidth;
  SEED = inSeed;
  MESHROW = HEIGHT/SEED; 
  MESHCOL = LENGTH/SEED; 

  // Dynamically declares an array, gets memory, assigns address to partMesh.
  partMesh = new int[MESHROW][MESHCOL]; 

  meshingPart(&partMesh, MESHROW, MESHCOL);
}


void PartClass :: meshingPart(int &partMesh, int inRow, int inCol)
{ 
  
  for( int i; i < inRow; i  )
  {
    for( int j; j < inCol; j  )
    {
      partMesh[i][j] = ElementClass();
    }
  }
}

CodePudding user response:

There are multiple problems with the shown code, not a single problem. All of the problems must be fixed in order to resolve all compilation errors.

void PartClass :: meshingPart(int &partMesh, int inRow, int inCol)

The first parameter to this class method is declared as a reference to a single, lonely int. It is not an array, hence the code in this class method that treats it as an array will make your C compiler very, very sad.

int *partMesh; //

partMesh = new int[MESHROW][MESHCOL]; 

partMesh is declared as a pointer to an int. The new expression produces, effectively, a pointer to an array of MESHCOL ints. In C you cannot convert a pointer to an array into a different kind of a pointer.

Furthermore, nothing shown here requires the use of new in the first place. partMesh could simply be a std::vector<vector<int>>, and the new replaced by a strategic resize(). As an extra bonus your C program will automatically delete all this memory when it is no longer needed, not leak it, and also implement correct copy/move semantics where needed!

meshingPart(&partMesh, MESHROW, MESHCOL);

As we've concluded, the first parameter to the function is a reference to an array. Passing to it an address of a pointer to int will also not work.

Furthermore, since partMesh is a member of the same class, having one function in the class pass, in some form, a member of the same class to another class method accomplishes absolutely useful, whatsoever. Since it's a member of the same class it doesn't need passing, the class method can access it directly. This is what, after all, classes are all about.

In conclusion:

  1. There are several problems regarding the C type system that are causing these compilation errors.

  2. It is not necessary to even use new here, to initialize the pointer, and either its type needs to be adjusted to reflect that it's a pointer to a 2D array, or the new statement itself needs to be adjusted to allocate a one-dimensional array, since that's the only thing C allows you to convert to a plain pointer. And even that is overnegineered, since a std::vector will take care of all these pesky details by itself.

  3. It is not necessary to even pass the member of the same class to the same class's method, as a parameter, just have the class method access it directly.

  4. It's apparent that the likely process that produced the shown code was to write it in its entirety, and only try to compile after the whole thing was written. An avalanche of compilation errors is almost guaranteed any time this approach is used. It is far more productive to write a large program by writing only a few lines at a time, testing them, make sure they work correctly, and only then write a few more. This way, the number of errors that need to be fixed will remain quite small, and manageable.

  • Related