简体   繁体   English

For-loop 语句 - 错误成员引用基类型 'int [13]' 不是结构或联合

[英]For-loop saying - ERROR Member reference base type 'int [13]' is not a structure or union

What is the problem with my for-loop?我的 for 循环有什么问题? It is inside a helper function but the error "Member reference base type 'int [13]' is not a structure or union" is happening across my code.它位于帮助器 function 内,但在我的代码中发生错误“成员引用基类型'int [13]'不是结构或联合”。 The 'int [13] changes to int[10] when I am using a 10 integer array, so I assume it is a problem there.当我使用 10 integer 数组时,'int [13] 更改为 int[10],所以我认为这是一个问题。 Here are two examples:这里有两个例子:

        int newisbn13[13];
        newisbn13[0] = 9;
        newisbn13[1] = 7;
        newisbn13[2] = 8;
        for (int p = 3; p < newisbn13.length() - 1; p++)
        {
            newisbn13[p] = isbn10[p-3];
        }

ERROR: Member reference base type 'int [13]' is not a structure or union错误:成员引用基类型“int [13]”不是结构或联合

Also:还:

int calc_check_digit_13(int input[], int size) {
    int sum = 0;
    for (int i = 0; i < input.length(); i++)
    {
        int tempnum = 0;
        if (i % 2 == 0)
        {
            tempnum = input[i];
        }
        else if (i % 2 == 1)
        {
            tempnum = input[i] * 3;
        }
        sum = tempnum + sum;
    }
etc. etc. etc.
}

ERROR: Member reference base type 'int *' is not a structure or union错误:成员引用基类型“int *”不是结构或联合

What is causing this error throughout my code?是什么导致我的代码中出现此错误? Thank you for your help.谢谢您的帮助。

newisbn13 is an array and in contrast to other languages like C# or Java it does not know its size. newisbn13是一个数组,与 C# 或 Java 等其他语言相比,它不知道它的大小。

You need to use sizeof(newisbn13) instead.您需要改用sizeof(newisbn13) Or since c++17 you can use std::size(newisbn13) .或者因为 c++17 你可以使用std::size(newisbn13)

However this will not work for calc_check_digit_13 .但是,这不适用于calc_check_digit_13 Because input will decay to a pointer and neither sizeof nor std::size will work there.因为input将衰减为指针,并且sizeofstd::size都不会在那里工作。 But probably the parameter size is what you want to use.但可能参数size是您想要使用的。

for (int i = 0; i < size; i++) {...}

For your first block of code, you make a call to a non-existent member function of type int .对于您的第一个代码块,您调用了一个不存在的int类型的成员 function 。 In C++, int is a primitive type and has no member functions or member variables.在 C++ 中, int是基本类型,没有成员函数或成员变量。

For the second block, you're calling the same function but on a pointer to an array of int s, so the type is int * and not int[13] , but its pretty much the exact same problem.对于第二个块,您调用的是相同的 function 但在指向int s数组的指针上,因此类型是int *而不是int[13] ,但它几乎是完全相同的问题。

As churill pointed out, you can use sizeof(int[]) or std::size(int[]) to find the number of elements in the array.正如 churill 指出的那样,您可以使用sizeof(int[])std::size(int[])来查找数组中的元素数。 If you need a container for integers, I would recommend using std::vector<int> to manage your int s.如果您需要一个整数容器,我建议您使用std::vector<int>来管理您的int This template class has tons of quality-of-life methods such as size() that can assist with what you might want to do.此模板 class 具有大量的生活质量方法,例如size()可以帮助您完成您可能想要做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM