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?