Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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!

share|improve this question
    
I would use a 1D array[m*n] and provide an 'operator () ( row, col) ' for mapping to 2D. – Dieter Lücking Jun 26 at 13:30

There are a few of things wrong with your code. Lets go through them one by one:

myarray(){grid=NULL;}=default

You can not implement this constructor and then tell C++ that you want the default one. You have to decide. Also, C++ has nullptr:

myarray(){ grid = nullptr; }

myarray(int& m,int& n) {
    grid = new char* [m];
    for(int i=0;i<n;i++) {
        grid[m]=new char [n];
    }
}

You are looping over the wrong variable! You should loop m times, as you have space for m char*s. Also, you want to assign the new char [n] to grid[i], not grid[m]:

myarray(int& m,int& n) {
    grid = new char* [m];
    for(int i=0;i<m;i++) {
        grid[i]=new char [n];
    }
}

~myarray(){delete [] grid;}

You only delete the char**, but never the char* you created. You have to store the dimensions somewhere and loop over the array again, deleting them one by one:

~myarray(){
    for(int i = 0; i < dimX; ++i) {
        delete[] grid[i];
    }
    delete[] grid;
}

Save dimX in a instancevariable on construction (m)


You'll end up with something like this:

class myarray
{
    char** grid;
    int dimX;
public:
    myarray(){ grid = nullptr; dimX = 0; }

    myarray(int& m,int& n) {
        grid = new char* [m];
        for(int i=0;i<m;i++) {
            grid[i]=new char [n];
        }
        dimX = m;
    }

    ~myarray(){
        for(int i = 0; i < dimX; ++i) {
            delete[] grid[i];
        }
        delete[] grid;
    }

    char** fetcharray(){ return grid; }
};
share|improve this answer
    
Works for me: cpp.sh/9ppcv – tkausl Jun 26 at 14:01
    
Thank you, I got it completely figured out! – ficabj5 Jun 26 at 14:32

Still no answer to your problem.. could you provide the full code (including ... and the full error) Maybe you try to access c(m,n) somewhere?

tkausl was quicker, so some more complete example.

class myarray
{
    char** grid;
    int dimX;
    int dimY;
public:
    myarray(){ grid = nullptr; dimX = 0; dimY=0; }

    myarray(int m,int n) { // don't use reference for this parameters, for complex types use pattern "const TYPE& name"
        dimX=m; dimY=n;
        grid = new char* [m];
        for(int i=0;i<m;i++) {
            grid[i]=new char [n];
        }
    }

    ~myarray(){
        for(int i = 0; i < dimX; ++i) {
            delete[] grid[i];
        }
        delete[] grid;
    }

    char** fetcharray(){ return grid; } // not really needed, avoid external access of internal data

    char at(int m, int n) const { 
        if(m<0 || m>=dimX || n<0 || n>=dimY) return 0; // check index bounds
        return grid[m][n]; 
    }
    char operator()(int m, int n) const { return at(m,n); } // overloading of () operator allows nice use

    char& at(int m, int n)  { 
        if(m<0 || m>=dimX || n<0 || n>=dimY)
        {
            // this is not nice, but ensures that always a writable memory is retured, 
            // may cause unecpected behavour, add breakpoint, trace, assert here
            // in production this shall never get exceuted
            static char dummy; 
            dummy=0;
            return dummy; 
        }
        return grid[m][n]; 
    }

    char& operator()(int m, int n)  { return at(m,n); }
    int sizeX()const { return dimX; }
    int sizeY()const { return dimY; }

};

usage like:

  myarray c(3,5);
  int x=c(2,1);
  c(2,1)=x+2;
  int y=c.at(2,1); // the const at variant
  c.at(2,1)=x+2;  // this is the non-const variant
share|improve this answer
    
I just edited the main question, and updated the code. Now you can take a look at it – ficabj5 Jun 26 at 17:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.