Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.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++ 模板函数崩溃_C++_Templates_C++11 - Fatal编程技术网

C++ 模板函数崩溃

C++ 模板函数崩溃,c++,templates,c++11,C++,Templates,C++11,我定义了以下模板函数,它在数组的每个元素上应用函数f template <typename X, typename Y, size_t n> array <Y, n> lod (const function <Y (const X)> f, const array <X, n> w) { array <Y, n> l; for (GLint i = 0; i = i + 1; i < n) l [

我定义了以下模板函数,它在数组的每个元素上应用函数f

template <typename X, typename Y, size_t n>     
array <Y, n> lod (const function <Y (const X)> f, const array <X, n> w)
{
   array <Y, n> l;
   for (GLint i = 0; i = i + 1; i < n)
      l [i] = f (w [i]);
   return l;
}
模板
数组lod(常量函数f,常量数组w)
{
数组l;
对于(闪烁i=0;i=i+1;i
作为f的一个例子,我定义了一个函数“adda”

模板
函数ae(常数X a)
{
返回[a](常数X X)
{
返回x+a;
};
}
然后我尝试应用它

const array <ivec3, 12> oblad = {ivec3 (0, 2, 1), ivec3 (0, 3, 2), ivec3 (0, 1, 5), ivec3 (0, 5, 4),
                                ivec3 (1, 2, 6), ivec3 (1, 6, 5), ivec3 (2, 3, 7), ivec3 (2, 7, 6),
                                ivec3 (3, 0, 4), ivec3 (3, 4, 7), ivec3 (4, 5, 6), ivec3 (4, 6, 7)};

array <ivec3, 12> w = lod (ae (ivec3 (1)), oblad);
常量数组扁率={ivec3(0,2,1),ivec3(0,3,2),ivec3(0,1,5),ivec3(0,5,4), ivec3(1,2,6),ivec3(1,6,5),ivec3(2,3,7),ivec3(2,7,6), ivec3(3,0,4),ivec3(3,4,7),ivec3(4,5,6),ivec3(4,6,7)}; 阵列w=lod(ae(ivec3(1)),扁率);

程序编译得很好,但是执行时会崩溃。

除了按值传递数组之外,崩溃的原因很简单:

for (GLint i = 0; i = i + 1; i < n)

conditionopt
是在循环的每个迭代开始时测试的条件,
expressionopt
是在每个迭代结束时执行的可选表达式。

一旦
i
达到
n
,循环就会写入数组的边界之外。您可能是指(GLint i=0;i这不是循环的工作方式。请参阅C++上的任何基本文本,投票时键入“键入”的“循环条件顺序”会喜欢编辑您的代码,但我不能,因为它只是一个“。”您的编辑中缺少:(@Zouch,它是标准的复制和粘贴:回滚-标准实际上是正确的(毫不奇怪),for init语句的定义包括它以分号结尾
for (GLint i = 0; i = i + 1; i < n)
for (GLint i = 0; i < n; i = i + 1)
for ( init-statement conditionopt ; expressionopt ) statement