4

I´m very new to C++ and so, I haven´t got any 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;
}

It´s a simple main-function which calls a function from my test_file.h which just includes a prototype: (test_file.h):

#ifndef TEST_FILE_H
#define TEST_FILE_H

int max(int a, int b);

#endif

And here´s my fitting source-file(test_file.cpp) which just includes 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 happens. The only thing I figured out, is that all will work fine if I copy the whole function into the test_file.h.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
MeineHTMLCodes
  • 509
  • 5
  • 19

0 Answers0