Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么可以';gcc是否推断数组参数的模板大小?(C+;+;11)_C++_Templates_Gcc_C++11 - Fatal编程技术网

C++ 为什么可以';gcc是否推断数组参数的模板大小?(C+;+;11)

C++ 为什么可以';gcc是否推断数组参数的模板大小?(C+;+;11),c++,templates,gcc,c++11,C++,Templates,Gcc,C++11,以下代码给出了一个编译器错误(gcc-4.7与-std=c++11一起运行): #include <iostream> #include <array> template <typename T, int N> std::ostream & operator <<(std::ostream & os, const std::array<T, N> & arr) { int i; for (i=0; i&

以下代码给出了一个编译器错误(gcc-4.7与
-std=c++11
一起运行):

#include <iostream>
#include <array>

template <typename T, int N>
std::ostream & operator <<(std::ostream & os, const std::array<T, N> & arr) {
  int i;
  for (i=0; i<N-1; ++i)
    os << arr[i] << " ";
  os << arr[i];
  return os;
}

int main() {
  std::array<double, 2> lower{1.0, 1.0};
  std::cout << lower << std::endl;
  return 0;
}

非常感谢您的帮助。

您可以致电
接线员考虑您的声明:

template <typename T, int N>
std::ostream & operator <<(std::ostream & os, const std::array<T, N> & arr) {

您使用的是
int
而不是
std::size\u t
,这就是它不匹配的原因。

请注意,您不应该定义
operator@K-ballo。您知道如何回答帖子中的两个编号问题中的任何一个吗?如果我可以回答,我会放置一个答案而不是注释。。。问题可能来自以错误的方式实现运算符。@K-ballo使用函数
模板打印(const std::array&arr){/*…*/}
可以提出同样的问题。我想知道为什么gcc不能绑定这个…你的意思是
print(更低)不会编译吗?你能把你的
打印版的测试用例添加到你的问题中吗?+1我不知道为什么我不试一下——我被
巨大的+1挂断了。非常感谢,我不确定我是否会发现这一点。+1我只是在阅读标准(14.8.2)中关于模板推断的内容,并想知道我遗漏了什么,因为没有任何东西反对这种推断。模板定义中不正确的类型也吸引了我。
operator<< <double,2>(std::cout, lower) << std::endl;
template <typename T, int N>
std::ostream & operator <<(std::ostream & os, const std::array<T, N> & arr) {
template<typename T, std::size_t N> class array {...};