错误:没有用于调用'variable'的匹配函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误:没有用于调用'variable'的匹配函数相关的知识,希望对你有一定的参考价值。

我正在学习c ++,我已经初始化了一个变量(chars向量的向量),然后添加了一个函数原型,然后在main中调用了该函数。

但我看到error: no matching function for call to 'std::vector<std::vector<char> >::vector(<brace-enclosed initializer list>)'

我认为错误信息是:

“嘿,你试图调用函数'std::vector<std::vector<char> >::vector(<brace-enclosed initializer list>)',但它不存在。”

但据我所知,我从未尝试将其称为函数,我只是尝试将其初始化为变量。

#include <iostream>
#include <vector>

int main()
{
    std::vector<std::vector<char>> board {{"5","3",".",".","7",".",".",".","."},{"6",".",".","1","9","5",".",".","."},{".","9","8",".",".",".",".","6","."},{"8",".",".",".","6",".",".",".","3"},{"4",".",".","8",".","3",".",".","1"},{"7",".",".",".","2",".",".",".","6"},{".","6",".",".",".",".","2","8","."},{".",".",".","4","1","9",".",".","5"},{".",".",".",".","8",".",".","7","9"}};
    return 0;
}
答案

定义std::vector的对象时,将选择适当的构造函数来初始化对象。编译器只是抱怨无法找到合适的构造函数。请注意,构造函数也是一个函数。

这里的问题是内部vector的元素类型是char,但"5""3"等是c风格的字符串,即const char[2]s,而不是chars。

将所有"更改为'

std::vector<std::vector<char>> board {{'5','3','.','.','7','.','.','.','.'},{'6','.','.','1','9','5','.','.','.'},{'.','9','8','.','.','.','.','6','.'},{'8','.','.','.','6','.','.','.','3'},{'4','.','.','8','.','3','.','.','1'},{'7','.','.','.','2','.','.','.','6'},{'.','6','.','.','.','.','2','8','.'},{'.','.','.','4','1','9','.','.','5'},{'.','.','.','.','8','.','.','7','9'}};

以上是关于错误:没有用于调用'variable'的匹配函数的主要内容,如果未能解决你的问题,请参考以下文章

C++问题 错误如下Time Check Failure #3 - The variable 'C' is being used without being initialized

”local variable 'xxx' referenced before assignment“python函数中改变外部定义数值时报错

遇到local variable 'e' referenced before assignment这样的问题应该如何解决

python 错误--UnboundLocalError: local variable '**' referenced before assignment

Unknown system variable 'query_cache_size' 错误原因以及解决办法

python 编程出现这个问题咋办 UnboundLocalError: local variable 'step' referenced before assignment