用构造函数和用户输入创建一个二维数组

时间:2016-06-26 13:13:53

标签: c++ arrays class matrix constructor

我被困住了。我希望能够获取用户输入,并使用构造函数创建公共2D数组,我的代码如下: `

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

`

正如我所说,我在“c(m,n);”行上收到错误,错误显示“错误:调用'(myarray)(int&amp;,int&amp;)'”不匹配“

如果我在本地的一个案例中声明c,程序就可以了,但我希望它可以在main()中访问

提前谢谢!

2 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。让我们逐一介绍它们:

myarray(){grid=NULL;}=default

您无法实现此构造函数,然后告诉C ++您想要默认的构造函数。你必须决定。此外,C ++还有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];
    }
}

你正在循环错误的变量!您应该循环m次,因为您有m char*的空间。此外,您要将new char [n]分配给grid[i],而不是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;}

您只会删除char**,但不会删除您创建的char*。您必须将维度存储在某处并再次循环遍历数组,逐个删除它们:

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

dimX保存在构造中的实例变量(m

你最终会得到这样的东西:

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

答案 1 :(得分:0)

仍然无法回答你的问题..你能否提供完整的代码(包括......和完整的错误) 也许你试图在某个地方访问c(m,n)?

tkausl更快,所以更完整的例子。

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

};

用法如:

  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