I'm a beginner and I'm currently feeling pretty lost about a compilation error that I'm getting since yesterday, so I was hoping someone could help me. I'm writing the following program:
#include <iostream>
#include <algorithm>
void heapify(int);
void print(int);
const int MAX = 10;
int main () {
int arr[MAX] = {2, 3, 4, 1, 12, 34, 11, 9, 22, 5};
print(arr[MAX]);
heapify(arr[MAX]);
print(arr[MAX]);
return 0;
}
void heapify(int arr[]) {
int i = 0;
int l = 2 * i;
int r = 2 * i 1;
for (i = 0; i < ::MAX; i ) {
if (l <= i/2 && arr[l] > arr[i/2]) {
std::swap(arr[l], arr[i/2]);
}
if (r <= i/2 && arr[r] > arr[i/2]) {
std::swap(arr[r], arr[i/2]);
}
}
}
void print(int arr[]) {
for (int i = 0; i < ::MAX; i ) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
I'm then compiling it using the g -c main.cpp and g -o main main.cpp commands but I get the following compilation errors:
main.o:main.cpp:(.text 0x66): undefined reference to 'print(int)'
main.o:main.cpp:(.text 0x72): undefined reference to 'heapify(int)'
main.o:main.cpp:(.text 0x7e): undefined reference to 'print(int)'
collect2.exe: error: ld returned 1 exit status
I've looked up the type of error and I get that whenever I am getting a compilation error like undefined reference to main c or undefined reference to a c function then I have to add definition of that function inside my file. I'm probably in the wrong but it seems to me that I have defined my functions correctly, though the program won't be executed successfully. I've consulted similar questions on StackOverflow about this error and, as far as I've seen, most of them have as a cause a missing .cpp file but I'm just compiling the main.cpp file so I don't really know what to do.
By any chance, can anyone point out what am I getting wrong?
CodePudding user response:
Look at the two declaration of heapify
.
The one before main
:
void heapify(int);
The one after main (which is also a definition):
void heapify(int arr[]) { ...
It might not be totally obvious, but these two declarations declare two different functions, both named heapify
but with different parameter types. One accepts an int
and another accepts an int *
(cleverly disguised as int []
-- in case of function parameters (but not elsewhere!) these two things are the same). Both can happily coexist in a single program. This is called "overloading". It is a powerful tool, but you don't need it here. You need one function named heapify
, not two.
The compiler complains about the first function, heapify(int)
, which is never defined.
So fix the first declaration to have the same parameter types as the other one:
void heapify(int []);
The same goes for print
.
Now if you try to compile your program, you will get compilation
errors. That's because the calls to heapify
and print
are wrong.
When you declare an array, you put the size in the square brackets:
int arr[MAX];
When you use and array, you don't put the size in the square brackets. To refer to a single element, you put its index in a square brackets:
int first = arr[0]; // for example.
To refer to the whole array, you don't use square brackets at all:
heapify(arr);
If you write arr[MAX]
, the following happens.
- The compiler treats
MAX
as just another index, no different from0
or1
orl/2
or anything else. - It generates code to access the element with the index
MAX
, which does not exist. Valid indices are0
toMAX-1
. Accessingarr[MAX]
is undefined behaviour. At run time the program would have crashed or produced unexpected results (or sometimes, by pure chance, expected results -- but only when you are looking at it, not when your professor is looking). So it's pretty bad. However... - The type of
arr[anything]
isint
, regardless of whether it exists or not. When you writeheapify(arr[MAX])
, the compiler generates a call toheapify(int)
. But this function was never defined. You definedheapify(int[])
which is a totally different function. And of course at the linking stageheapify(int)
is not found.
Fix the calls to heapify
and print
and the program should compile. (I have not tested it so I don't know if it works).