C ++ beginner: Why doesn't the compiler find my functions in the header?

I am very new to C ++, and therefore I did not have experience with HeaderFiles. I have the following files ( main.cpp ):

#include <iostream>
#include "test_file.h"

int main(){
   std::cout << max(3,4) << std::endl;
   return 0;
}

This is a simple main function that calls a function from my test_file.h that only includes the prototype: ( test_file.h ):

#ifndef TEST_FILE_H
#define TEST_FILE_H

int max(int a, int b);

#endif

And here is my source file ( test_file.cpp ), which includes only the final function:

#include "test_file.h"

int max(int a, int b){
   return a>b ? a:b;
}

Every time I run this, the same error occurs:

undefined reference to `max(int, int)'
collect2.exe: error: ld returned 1 exit status

I just don’t know why this is happening. The only thing that I understood is that everything will work fine if I copy the entire function to test_file.h.

+4
source share

Source: https://habr.com/ru/post/1690862/


All Articles