gpt4 book ai didi

c++ - 错误 : no matching function for call to 'variable'

转载 作者:行者123 更新时间:2023-11-28 01:25:23 25 4
gpt4 key购买 nike

我正在学习 C++,我已经初始化了一个变量(字符 vector 的 vector ),然后添加了一个函数原型(prototype),然后在 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],而不是 char

将所有的"改为'

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

关于c++ - 错误 : no matching function for call to 'variable' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54067839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com