Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the problematic code:
"test.h":
C++
static bool All(bool* iterable);

"test.cpp":
C++
#include "test.h"
static bool All(bool* iterable) {
    for (int i = 1; i < (sizeof(iterable)/(sizeof(*iterable))); i++) {
        if (iterable[i] != iterable[0]) {
            return false;
        }
    }
    return true;
}

"main.cpp":
C++
#include "test.h"

int main(void) {
  bool test[] = { true, true, true };
  All(test);
  return 0;
}

And it gives me:
C++
undefined reference to 'All(bool*)'

When i try to compile. I am using g++ through a Makefile to compile if that helps.

What I have tried:

I tried moving the code to the .h file, it compiled but i have heard that is bad to do, presumably for performance.
Posted
Updated 20-Nov-21 16:05pm
v2

The others have given you some direction to solve your issue, but are you aware there's a bug in your code? You have
bool All(bool *iterable) {
    for(int i = 1; i < sizeof(iterable)/sizeof(*iterable); ++i) { 

C++ does not carry around array sizes, so sizeof(iterable) will be the size of a pointer on your system - probably 4 or 8 bytes. In this case sizeof(*iterable) equals sizeof(bool) which is 1. So the loop index will go from 1 to <bytes in="" pointer="">, regardless of how the array is that iterable points to.

The use of the sizeof division only works with arrays of known size at compile time e.g.
C++
double  d[]{ 1.0, 2.5, 3.14, 4.7, 5.0, 6.9, 17};  // d has 7 elements
size_t d_len = sizeof(d)/sizeof *d;  // d_len = 7
 
Share this answer
 
Outside of classes/structs, static in c++ means local to the file in which it is defined (i.e., inaccessible to outside world).
If the All method is not in a class, then delete the static storage qualifier from the method declaration, and all should be fine.
 
Share this answer
 
Try removing static from the declaration and definition of All in test.h and test.cpp. Unless a function is a class member, defining it as static means that nothing outside the .cpp can see it. It will therefore appear to be declared in the interface but left unimplemented as far as main.cpp is concerned.
 
Share this answer
 
v2
Comments
Member 14769677 18-Nov-21 23:13pm    
I tried making it non-static, but it still won't compile. Same reason. I also tried to do what i would have done in C#, and put a public in front of it, didn't work either. Is this where i am supposed to use the extern keyword? How would i do that if that is the case?
Greg Utas 19-Nov-21 6:02am    
Yes, you could use extern, though I don't think it should be necessary. In test.h, you would use "extern" where you had used "static". In test.cpp, you'd use neither.
Member 14769677 19-Nov-21 7:44am    
Extern didn't work either, i find this really wierd and confusing xD
Greg Utas 19-Nov-21 8:08am    
Sorry that I couldn't resolve this. I don't have any more suggestions to offer.
Member 14769677 19-Nov-21 12:07pm    
Thank you for trying!
Just to elaborate on what k5054 wrote, there is a macro definition in Visual C++ that implements the sizeof division you have and it is called _countof. Its implementation looks like this :
C++
#define _countof(array) (sizeof(array) / sizeof(array[0]))
and it is documented here : _countof Macro | Microsoft Docs[^]. I don't know if it is available in other compilers but if not the definition given above can be utilized.
 
Share this answer
 
Comments
Member 14769677 19-Nov-21 7:42am    
Thank you!
You might have identify the problem in your code and Solution 3 stats a solution as well. But it will only work when you have the known number of elements of your array pre-hand.

On the other hand, you can maintain a Count variable which will be incremented/decremented for every insertion/deletion to the array. You can pass this variable to your ALL() function which then should use this variable in the loop. like

#include "test.h"
static bool All(bool* iterable, int count) {
    for (int i = 1; i < count; i++) {
        if (iterable[i] != iterable[0]) {
            return false;
        }
    }
    return true;
}
 
Share this answer
 
I fixed it. I have to add the file in the makefile. Slight oversight, hehe.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900