jeudi 27 janvier 2022

Getting compilation errors when trying to use pointers for dynamically allocated 2D array

I've recently started learning coding in the C++ language, and am having issues grasping the concept of pointers in C++.

I'm trying to practice dynamic memory allocation but keep running into errors when trying to compile my program.

Here are some struct objects I've created along with some global variables outside of my main function:

// Storing x and y values from file
struct gridSize {
    int gridXValue;
    int gridYValue;
    
    string toString();
};

string gridSize::toString ()
{
    ostringstream oss;

    oss << "gridXValue : " << gridXValue << endl;
    oss << "gridYValue : " << gridYValue << endl;

    return (oss.str());
}

// Storing details for each grid area within
// 2D array struct
struct grid_area {
    int x, y;
    bool is_occupied;
    int city_id;
    string city_name;
    int cloud, cloud_index, pressure, pressure_index;
    char cloudcover_LMH, pressure_LMH;
};

gridSize gridSize; //Global struct object
grid_area *ga = nullptr; //Global pointer variable

Here is what my main function looks like:

int main ()
{
    int userChoice;

    // Main menu to run constantly until user enters 8
    while (userChoice != 8) {
        cout << "Welcome to Weather Information Processing System!" << endl;
        cout << "-------------------------------------------------" << endl;

        cout << endl;

        cout << "1)\tRead in and process a configuration file" << endl;
        cout << "2)\tDisplay city map" << endl;
        cout << "3)\tDisplay cloud coverage map (cloudiness index)" << endl;
        cout << "4)\tDisplay cloud coverage map (LMH symbols)" << endl;
        cout << "5)\tDisplay atmospheric pressure map (pressure index)" << endl;
        cout << "6)\tDisplay atmospheric pressure map (LMH symbols)" << endl;
        cout << "7)\tShow weather forecast summary report" << endl;
        cout << "8)\tQuit" << endl;

        cout << endl;

        cout << "Please enter your choice: ";
        cin >> userChoice;

        cout << endl;

        if (userChoice == 1) {
            // Process all files
            readAFile();
        }
    }
        
        // Deallocate memory for 2D array
        for (int i = 0; i < gridSize.gridXValue + 1; i++) 
        {
            delete[] ga[i];
        }
        delete[] ga;
        
    
    return (0);
}

I highly suspect the issue lies in the last few portions of my readAFile function, but am unable to identify the problem. Here's the portion where I suspect the problem lies in readAFile:

    // Allocate array memory
    ga = new grid_area [gridSize.gridXValue + 1];
    for (int i = 0; i < gridSize.gridYValue + 1; i++)
        ga[i] = new grid_area[gridSize.gridYValue + 1];

    // Putting x and y values into  array
    for (int x_array = 0; x_array < gridSize.gridXValue + 1; x_array++) {
        for (int y_array = 0; y_array < gridSize.gridYValue + 1; y_array++) {
            ga[x_array][y_array].x = x_array;
            ga[x_array][y_array].y = y_array;
        }
    }

This is error I'm getting:

csci251_a1.cpp: In function ‘int main()’:
csci251_a1.cpp:110:20: error: type ‘struct grid_area’ argument given to ‘delete’, expected pointer
       delete[] ga[i];
                    ^
csci251_a1.cpp: In function ‘void readAFile()’:
csci251_a1.cpp:172:54: error: no match for ‘operator=’ (operand types are ‘grid_area’ and ‘grid_area*’)
         ga[i] = new grid_area[gridSize.gridYValue + 1];
                                                      ^
csci251_a1.cpp:32:8: note: candidate: ‘grid_area& grid_area::operator=(const grid_area&)’
 struct grid_area {
        ^~~~~~~~~
csci251_a1.cpp:32:8: note:   no known conversion for argument 1 from ‘grid_area*’ to ‘const grid_area&’
csci251_a1.cpp:32:8: note: candidate: ‘grid_area& grid_area::operator=(grid_area&&)’
csci251_a1.cpp:32:8: note:   no known conversion for argument 1 from ‘grid_area*’ to ‘grid_area&&’
csci251_a1.cpp:177:24: error: no match for ‘operator[]’ (operand types are ‘grid_area’ and ‘int’)
             ga[x_array][y_array].x = x_array;
                        ^
csci251_a1.cpp:178:24: error: no match for ‘operator[]’ (operand types are ‘grid_area’ and ‘int’)
             ga[x_array][y_array].y = y_array;

I really don't understand what I'm doing wrong here... If needed, I can provide the rest of the readAFile function as well. Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire