mercredi 29 juin 2016

Constructor not working "error: no match for call to '(<class name>) (int&, int&)"

I have written a simple program which has several possible inputs(i,c,l,v..). The first input is "i m n", where m and n are integer values. This command generates a 2D array with m rows and n cols. Here is my code:

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;}

However, I get an error in case 'i': ...; c(m,n); saying "error: no match for call to '(myarray) (int&, int&)'". When I declare the variable myarray c; in case as a local variable (myarray c(m,n)) everything works just fine. However, I want the variable c to be accessible by other cases and therefore need it to be available throughout the main() function, as it is in the code above. Does anyone know what is wrong and how can I fix it? Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire