我试图编写一个数独程序来验证已完成的棋盘。这是我到目前为止的代码:无法将'int *'转换为'int *(*)[9]'作为参数'1'
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <pthread.h>
using namespace std;
int *board[9];
int row, col;
void is_row_ok(int* board[9][9]);
void is_col_ok(int* board[9][9]);
void is_square_ok(int* board[9][9]);
int main()
{
for (int i = 0; i < 9; ++i)
{
board[i] = new int[9];
}
string line;
ifstream myFile("Testfile1.txt");
for (int row = 0; row < 9; ++row)
{
string line;
getline(myFile, line);
stringstream iss(line);
cout << endl;
for (int col = 0; col < 9; ++col)
{
string val;
getline(iss, val, ',');
if (!iss.good())
break;
stringstream convertor(val);
convertor >> board[row][col];
cout << board[row][col] << " ";
}
}
is_row_ok(&board[9][9]); //<-- error happens here
pthread_create(thread1, NULL, is_row_ok, board[9][9]);
//pthread_create(thread1, NULL, is_col_ok, board[9][9]);
//pthread_create(thread1, NULL, is_square_ok, board[9][9]);
cout << endl;
return 0;
}
void is_row_ok(int board[9][9])
{
int element_count = 0;
char element_value;
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
element_count = 0;
element_value = board[i][j];
if (element_value != ' ')
{
for (int k = 0; k < 9; ++k)
{
if (board[i][k] == element_value)
element_count++;
}
}
if (element_count >= 2)
{
cout << "Row " << i << " is invalid." << endl;
}
else
{
cout << "Row " << i << " is valid." << endl;
}
}
}
//pthread_exit(NULL);
}
,我发现了以下错误:
attempt.cpp:45:24: error: cannot convert ‘int*’ to ‘int* (*)[9]’ for argument ‘1’ to ‘void is_row_ok(int* (*)[9])’
is_row_ok(&board[9][9]);
我不知道发生了什么事。我在这里和其他网站上看到了一堆类似的情况,我试图实现他们的解决方案,但都没有工作。如果任何人可以请帮助我找出我需要改变,这将不胜感激。
P.S.函数is_col_ok()和is_square_ok()与is_row_ok()函数非常相似,因此它们不包含在内。
P.P.S.如果你也可以看看我的pthread创建函数,并告诉我我是否已经正确地完成了它,如果不是,我需要更改
P.P.S.如果您需要任何额外的代码,请不要犹豫,要求它
发生此错误的原因是:board [9] [9]'解析为int类型的值。所以你把这个地址取出来,然后用一个指向'int'的指针,也就是'int *'。这与您尝试调用的函数期望的参数的值类型不匹配,因此编译器错误。 – user268396
'is_row_ok(&board [9] [9]);' - 你想说什么? – AnT
http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – stark