jeudi 30 juin 2016

Making a 2d array with constructor and user input

I am stuck. I want to be able to take user input, and make a public 2D array using a constructor, my code goes as follows: `

class myarray
{
    char** grid;
    int dimX,dimY;
public:
    myarray(){grid=0;}
    myarray(int m,int n) {grid = new char* [m]; for(int i=0;i<m;i++) {grid[i]=new char [n];} dimX=m; dimY=n;}
    ~myarray(){for(int i = 0; i < dimX; ++i) {delete[] grid[i];} delete[] grid;}
    char** fetcharray(){return grid;}

int main()
{
    srand(time(NULL));
    bool check(true),arrayinitialized(false);
    while(check)
    {
        char a; //a-firstinp;
        int m,n; //m,n-grid size

        cin>>a;

        myarray c;

        switch(a)
        {
        case 'i':
        case 'I': {cin>>m>>n;
                  c(m,n);
                  arrayinitialized=true;
                  break;}
        case ...:...
        default:{cout<<"Invalid input! Try again: "; break;}

`

As I have said, I get an error on line "c(m,n);", an error says "error: no match for call to ‘(myarray) (int&, int&)’"

Program works just fine if I declare c in one of the cases locally, however I want it to be accessible throughout main()

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire