c++make_pair找不到匹配的函数
c++ make_pair no matching function found
我有以下代码
#include <utility>
using namespace std;
int a = ...//gets calculated somehow
int b = ...//same here
char container[a][b];
...
std::pair<int, char**> p = make_pair(1, container);
最后一行给我
main.cpp:10:58: error: no matching function for call to 'make_pair(int, char [a][b])'
std::pair<int, char**> p = make_pair(1, container);
^
main.cpp:10:58: note: candidate is:
In file included from /usr/local/include/c++/4.9.2/utility:70:0,
from main.cpp:1:
/usr/local/include/c++/4.9.2/bits/stl_pair.h:276:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&)
make_pair(_T1&& __x, _T2&& __y)
^
/usr/local/include/c++/4.9.2/bits/stl_pair.h:276:5: note: template argument deduction/substitution failed:
main.cpp:10:58: note: variable-sized array type 'char (&)[a][b]' is not a valid template argument
std::pair<int, char**> p = make_pair(1, container);
^
main.cpp:10:32: warning: unused variable 'p' [-Wunused-variable]
std::pair<int, char**> p = make_pair(1, container);
您的问题有两个方面。首先,静态分配数组的维度必须是编译时常数。可变长度数组(VLA)是非标准的,但某些编译器支持它作为扩展。不过,为了保持程序的标准一致性和可移植性,我会远离这些。
使a
和b
成为const
就足够了:
int const a = 5;
int const b = 10;
char container[a][b];
接下来,p
的类型与从make_pair(1, container)
返回的类型不匹配。在内部,make_pair
"衰减"推导出的参数类型(在这种情况下应用数组到指针的转换)。第二个参数container
的衰减类型并没有变成char**
,而是变成了char (*)[3]
——指向第一个元素的指针,而第一个元素本身就是一个数组。
因此,将该行更改为该行应该有效:
std::pair<int, char (*)[3]> p = std::make_pair(1, container);
您可能还想使用auto
,这样类型推导就可以减轻混淆:
auto p = std::make_pair(1, container);
并考虑使用std::vector<std::string>
而不是C样式的数组。
相关文章:
- 编译器找不到'aligned_alloc'函数
- 在 c++ 中找不到此函数的标识符
- LLVM 选择找不到函数传递
- 找不到 #define 的函数定义
- LD 找不到生成的函数
- C++线程找不到函数作为参数(链接器)
- 为什么 std::sort 找不到合适的(静态成员)函数重载?
- Javascript 找不到使用 emscripten 编译的导出 WASM 函数
- 给定一个 3 x 3 矩阵或任何 n x n 矩阵,为什么我的以下函数找不到对角线差异?
- QObject::连接不起作用 - 使用函数语法找不到信号
- VS 2017 C++编译器在 VS 2005 中找不到匹配函数
- 找不到如何创建使用指针、字符和 for 函数组合的程序
- 这个基于范围的'for'语句需要一个合适的函数,但找不到任何函数
- im调用函数时找不到标识符?
- 找不到使用 bitset 实例化模板函数的有效方法
- 如何修复找不到匹配的重载函数?
- ADL 找不到重载函数
- 构造函数找不到合适的定义,以及 100 个其他错误?
- 类函数找不到类定义的变量
- 平均函数找不到正确的平均c++