我正在构建一个有趣的C ++项目,我有以下错误:没有匹配函数来调用' loopAction'我花了大约1.5个小时试图解决这个问题并通过Stack溢出来解读可能存在的问题。我希望额外的一双眼睛能帮助我找到问题。
我的程序中有以下原型:
//Global Constants
const int ROWS = 100;
const int COLS = 100;
void loopAction(fstream &, int [][COLS], int, int, int);
在上面的例子中,fstream是一个文件对象,int [] []是一个二维数组,最后三个值是变量。
我声明的变量:
ifstream File;
File.open("deskInfo.txt");
int n, m;
char theValues[ROWS][COLS];
这是对我的功能的调用:
loopAction(File, theValues, ROWS, n, m);
实际功能:
void loopAction(fstream &file, int values[][COLS], int rows, int n, int m){
char row;
for (int r = 0; r < n; r++){
for (int c = 0; c < m; c++){
file >> row;
values[r][c] = row;
}
}
}
如果需要更多信息,请与我们联系。完整的错误消息是:
语义问题,没有匹配函数来调用&{39;
loopAction
&#39; =&GT; 候选功能不可行:没有来自&#39;ifstream
&#39; (又名&#39;basic_ifstream<char>
&#39;)到&#39;fstream &
&#39; (又名&#39;basic_fstream<char> &
&#39;)第一个参数
答案 0 :(得分:4)
theValues
输入为char[][]
,但方法签名接受int[][]
。
与原始问题无关的其他一些提示:
const
至#define
File
的{{1}}。通过接受数组类型(带大小)作为模板参数,可以防止数组衰减(foo[N]
如何转向foo*
):
template<typename TArray,int length>
void loopAction(fstream& file, TArray& values[length], size_t n, size_t m)
这样您就不需要使用ROWS
和COLS
,您可以正确使用sizeof
。
答案 1 :(得分:0)
我认为编译器错误消息说明了一切。该函数需要fstream
,但您传递ifstream
。 ifstream
并非来自fstream
。有关详细信息,请参阅http://www.cplusplus.com/reference/fstream/ifstream/。