STL 复制在"typedef"类型的数组上失败

STL copy failed on array of 'typedef' type

本文关键字:数组 失败 typedef 复制 STL 类型      更新时间:2023-10-16

平台:MinGW64(ruenvb 4.7.2(,Windows 7(64(,Qt 4.8.2

给定代码段如下:

/* type definition */
typedef long T_PSIZE;
struct A { T_PSIZE myArray[10]; };
struct B { T_PSIZE myArray[10]; };
/* declare variable */
A a;
B b;
std::copy(a.myArray[0], a.myArray[10], &b.myArray);

我不知道编译器为什么会抛出以下错误消息(当从"typedef long t_PSIZE;"更改为"type def int t_PSIZEs;"时也会显示类似的消息(:

> c:mingwrubenvb-4.7.2-64bin../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:
> In instantiation of '_OI std::__copy_move_a(_II, _II, _OI) [with bool
> _IsMove = false; _II = long int; _OI = long int (*)[9]]': c:mingwrubenvb-4.7.2-64bin../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:422:39:
> required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool
> _IsMove = false; _II = long int; _OI = long int (*)[9]]' c:mingwrubenvb-4.7.2-64bin../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:454:18:
> required from '_OI std::copy(_II, _II, _OI) [with _II = long int; _OI
> = long int (*)[9]]' ..InfluSimHKPrototypeSimApplication.cpp:114:36:   required from here
> c:mingwrubenvb-4.7.2-64bin../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:375:57:
> error: no type named 'value_type' in 'struct std::iterator_traits<long
> int>'
> c:mingwrubenvb-4.7.2-64bin../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:377:64:
> error: no type named 'iterator_category' in 'struct
> std::iterator_traits<long int>'
> c:mingwrubenvb-4.7.2-64bin../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:381:57:
> error: no type named 'value_type' in 'struct std::iterator_traits<long
> int>'
> c:mingwrubenvb-4.7.2-64bin../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_algobase.h:384:70:
> error: no type named 'iterator_category' in 'struct
> std::iterator_traits<long int>'

编译器的模板引擎似乎无法识别类型"long int"。我在"普通int"数组中使用了类似的语句,效果很好。我没有使用任何STL容器,因为我确切地知道目标数组的大小,所以我认为我不需要重新实现类似back_inserter的东西。我错过什么了吗?

注意:我不确定这个问题是否有帮助。(或者,我如何才能获得语句的"完整"限定名来处理typedef-ed变量?(

您的意思可能是:

std::copy(&a.myArray[0], &a.myArray[10], &b.myArray[0]);

std::copy(a.myArray, a.myArray + 10, b.myArray);

a.myArray[0]只是long,而不是std::copy所需的指向long数组的指针。此外,输出参数的类型需要与要复制的对象的类型兼容。&b.myArray具有类型long (*)[10],而您需要提供类型long*的参数。