'char(* RtlpNumberOf(T(&)[N]))[N]':无法从'char *'推论出'T(&)[N]'的模板参数

时间:2018-06-28 01:58:02

标签: c++

我最初写的是这段代码,它将10个const char[]组合成一个大char[]。很好。

char total[1000000];
memcpy(total, myData0, ARRAYSIZE(myData0));
memcpy(&total[ARRAYSIZE(myData0)], myData1, ARRAYSIZE(myData1));
memcpy(&total[ARRAYSIZE(myData0) + ARRAYSIZE(myData1)], myData2, ARRAYSIZE(myData2));
memcpy(&total[ARRAYSIZE(myData0) + ARRAYSIZE(myData1) + ARRAYSIZE(myData2)], myData3, ARRAYSIZE(myData3));
memcpy(&total[ARRAYSIZE(myData0) + ARRAYSIZE(myData1) + ARRAYSIZE(myData2) + ARRAYSIZE(myData3)], myData4, ARRAYSIZE(myData4));
memcpy(&total[ARRAYSIZE(myData0) + ARRAYSIZE(myData1) + ARRAYSIZE(myData2) + ARRAYSIZE(myData3) + ARRAYSIZE(myData4)], myData5, ARRAYSIZE(myData5));
memcpy(&total[ARRAYSIZE(myData0) + ARRAYSIZE(myData1) + ARRAYSIZE(myData2) + ARRAYSIZE(myData3) + ARRAYSIZE(myData4) + ARRAYSIZE(myData5)], myData6, ARRAYSIZE(myData6));
memcpy(&total[ARRAYSIZE(myData0) + ARRAYSIZE(myData1) + ARRAYSIZE(myData2) + ARRAYSIZE(myData3) + ARRAYSIZE(myData4) + ARRAYSIZE(myData5) + ARRAYSIZE(myData6)], myData7, ARRAYSIZE(myData7));
memcpy(&total[ARRAYSIZE(myData0) + ARRAYSIZE(myData1) + ARRAYSIZE(myData2) + ARRAYSIZE(myData3) + ARRAYSIZE(myData4) + ARRAYSIZE(myData5) + ARRAYSIZE(myData6) + ARRAYSIZE(myData7)], myData8, ARRAYSIZE(myData8));

但是我想我可以使用数组并循环遍历它。

char total[1000000];
char* myarray[10] = { myData0, myData1, myData2, myData3, myData4, myData5, myData6, myData7, myData8, myData9 };

for (int i = 0; i < ARRAYSIZE(myarray); i++)
{
    memcpy(total, &myarray[i], ARRAYSIZE(myarray[i]));
}

我以为我没事,但是这段代码无法编译。我在ARRAYSIZE(myarray[i])下收到一个错误,内容为'char (*RtlpNumberOf(T (&)[N]))[N]': could not deduce template argument for 'T (&)[N]' from 'char *'。我不明白这里是什么问题。我尝试另外使用sizeof,但没有正确复制。为什么在这种情况下我无法使用ARRAYSIZE?

2 个答案:

答案 0 :(得分:1)

在第一段代码中,您正在实际数组上调用ARRAYSIZE。在第二个中,您将传递一个char *到它。该宏(实际上是作为其基础的RtlpNumberOf宏)需要一个数组。数组和指针不是一回事,这就是一个完美的例子。

创建另一个数组,该数组具有其他数组的大小,并在调用memcpy时使用它:

char total[1000000];
char* myarray[10] = { myData0, myData1, myData2, myData3, myData4, 
                      myData5, myData6, myData7, myData8, myData9 };
size_t myarray_sizes[10] = { ARRAYSIZE(myData0), ARRAYSIZE(myData1), ARRAYSIZE(myData2), 
                             ARRAYSIZE(myData3), ARRAYSIZE(myData4), ARRAYSIZE(myData5), 
                             ARRAYSIZE(myData6), ARRAYSIZE(myData7), ARRAYSIZE(myData8), 
                             ARRAYSIZE(myData9) };


for (int i = 0; i < ARRAYSIZE(myarray); i++)
{
    memcpy(total, &myarray[i], myarray_sizes[i]);
}

答案 1 :(得分:0)

或者这样:

#include <iostream>

int main () {
    char total[1000000];
    char * myData0 = "this ";
    char * myData1 = "is ";
    char * myData2 = "a ";
    char * myData3 = "test1 ";
    char * myData4 = "test2 ";
    char * myData5 = "test3 ";
    char * myData6 = "test4 ";
    char * myData7 = "test5 ";
    char * myData8 = "test6 ";
    char * myData9 = "test7 ";

    char* myarray[10] = { myData0, myData1, myData2, myData3, myData4, myData5, myData6, myData7, myData8, myData9 };

    for (int i = 0; i < 10; i++)
    {
        strcat(total, myarray[i]);
    }

    std::cout << total << std::endl;

}