C++ c++;make_pair未找到匹配函数
我有以下代码C++ c++;make_pair未找到匹配函数,c++,std-pair,C++,Std Pair,我有以下代码 #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, contai
#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);
#包括
使用名称空间std;
int a=…//以某种方式进行计算
int b=…//此处相同
炭容器[a][b];
...
std::pair p=制造_对(1,容器);
最后一行告诉我
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);
main.cpp:10:58:错误:调用“make_pair(int,char[a][b])没有匹配函数
std::pair p=制造_对(1,容器);
^
main.cpp:10:58:注:候选人为:
在/usr/local/include/c++/4.9.2/utility:70:0中包含的文件中,
来自main.cpp:1:
/usr/local/include/c++/4.9.2/bits/stl_pair.h:276:5:注:模板constepr std::pair std::make_pair(_T1&,_T2&)
配对(T1和x、T2和y)
^
/usr/local/include/c++/4.9.2/bits/stl_pair.h:276:5:注意:模板参数推导/替换失败:
main.cpp:10:58:注意:可变大小的数组类型“char(&)[a][b]”不是有效的模板参数
std::pair p=制造_对(1,容器);
^
main.cpp:10:32:警告:未使用的变量“p”[-Wunused variable]
std::pair p=制造_对(1,容器);
您的问题是双重的。首先,静态分配数组的维数必须是编译时常量。可变长度数组(VLA)是非标准的,但某些编译器支持它作为扩展。不过,为了保持程序的标准一致性和可移植性,我会远离这些
制作a
和b
const
就足够了:
int const a=5;
int const b=10;
炭容器[a][b];
接下来,p
的类型与从make\u pair(1,容器)
返回的类型不匹配。在内部,make_pair
“衰减”推导出的参数类型(在这种情况下应用数组到指针的转换)。第二个参数的衰减类型container
不会变成char**
,而是char(*)[3]
——指向第一个元素的指针,该元素本身就是一个数组
因此,将该行更改为该行应该有效:
std::pair p=std::make_pair(1,容器);
您可能还希望使用auto
,以便类型推断可以减轻混淆:
auto p=std::make_对(1,容器);
并考虑使用<代码> STD::vector < /COD>而不是C样式数组。
不应该是“代码> STD::MaMaGION/<代码>?跟我说:数组不是指针。哦,请不要使用VLAS…使用向量并停止使用指针/数组混合,您将没有problems@aschepler:如果错误是关于将一对从一种类型转换为另一种类型,那么这可以解释。但是错误不应该是关于错误的转换吗?我应该如何设置类型?实际上,在这种情况下,VLA也会导致make\u pair
失败-这不是转换失败。