-3

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!

ficabj5
  • 111
  • 10

1 Answers1

1
myarray c;    
c(m, n);

- the second line requires myarray to have operator(). It won't call the constructor. (again?)

Try

myarray c(m, n);

at the point of usage, or make myarray properly copyable and

c = myarray(m, n);

I remember some folks telling you that the default constructor is broken, as well as copy assignment... The Rule of three. So go ahead!


I want the variable c to be accessible by other cases

Then it should be outside the loop.

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74