函数原型中的多维数组

时间:2012-11-01 21:10:34

标签: c++ class multidimensional-array

我有这样的代码:

class oBT{
    public: void clean(int**);
            void write(int**);
            bool check(int**);
            void backtrack(int**,int);
};

void oBT::clean(int R[8][8])
{ for(int i=1; i<=8; i++) for(int j=1; j<=8; j++) R[i][j]=0; }

void oBT::write(int R[8])
{
    for(int i=1; i<=8; i++)
    {
        for(int j=1; j<=8; j++)
            std::cout<<R[i]<<' ';
        std::cout<<'\n';
    }
    std::cout<<'\n';
}

bool oBT::check(int R[8], int i=1)
{
    for(int j=1; j<=8; j++)
    {
        for(int k=1; k<=8; k++)
        {
            if(R[i]==R[j]) return false;
            if(R[j]==R[j]) return false;
        }
        i++;
    }
    return true;
}

void oBT::backtrack(int R[8], int i=1)
{
    for(int j=1; j<=8; j++)
        for(int k=1; k<=8; k++)
        {
            R[j]=1;
            if(check(R))
                if(i<8) backtrack(R,i+1);
                else { write(R); clean(R); }
        }
}

当我尝试编译它时,我收到以下错误:

C:\OJI\Eight Queen Puzzle\Class.h|8|error: prototype for 'void oBT::clean(int (*)[8])' does not match any in class 'oBT'|
C:\OJI\Eight Queen Puzzle\Class.h|2|error: candidate is: void oBT::clean(int**)|
C:\OJI\Eight Queen Puzzle\Class.h|11|error: prototype for 'void oBT::write(int*)' does not match any in class 'oBT'|
C:\OJI\Eight Queen Puzzle\Class.h|3|error: candidate is: void oBT::write(int**)|
C:\OJI\Eight Queen Puzzle\Class.h|22|error: prototype for 'bool oBT::check(int*, int)' does not match any in class 'oBT'|
C:\OJI\Eight Queen Puzzle\Class.h|4|error: candidate is: bool oBT::check(int**)|
C:\OJI\Eight Queen Puzzle\Class.h|36|error: prototype for 'void oBT::backtrack(int*, int)' does not match any in class 'oBT'|
C:\OJI\Eight Queen Puzzle\Class.h|5|error: candidate is: void oBT::backtrack(int**, int)|

||=== Build finished: 8 errors, x warnings ===|

1 个答案:

答案 0 :(得分:3)

数组不是指针,指针不是数组。如果你想传入多维数组,可以使用双指针和动态内存分配(即new),或者声明你的函数接受一个数组数组,比如

void clean(int parm[8][8]);

此外,您的check函数有'拼写错误',您从原型中省略了'size'参数;而不是

bool check(int **);

应该是

bool check(int R[8][8], int i);