Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
What is the problem with my for-loop? 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. The 'int [13] changes to int[10] when I am using a 10 integer array, so I assume it is a problem there. 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
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
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.
You need to use sizeof(newisbn13)
instead.
Or since c++17 you can use std::size(newisbn13)
.
However this will not work for calc_check_digit_13
. Because input
will decay to a pointer and neither sizeof
nor std::size
will work there. But probably the parameter size
is what you want to use.
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
. In C++, int
is a primitive type and has no member functions or member variables.
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.
As churill pointed out, you can use sizeof(int[])
or std::size(int[])
to find the number of elements in the array. If you need a container for integers, I would recommend using std::vector<int>
to manage your int
s. This template class has tons of quality-of-life methods such as size()
that can assist with what you might want to do.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.