How to Fix the Multiple Definitions of a Function Error in C++

Muhammad Husnain Feb 02, 2024
How to Fix the Multiple Definitions of a Function Error in C++

This article is about the solution to the frequently occurring error in C++, which is multiple definitions of a function.

Fix the multiple definitions of a function Error in C++

Such error is usually caused when we try to separate the function prototype and its definition. Therefore, it is recommended that you separate the prototypes and definitions in different files and include the file accordingly.

Consider the example below to understand the problem.

Example code (file1.cpp):

// file1.cpp
#include <iostream>
using namespace std;

class classB {
  friend void f1();

 public:
  classB(int i = 1, int j = 2) : a(i), b(j) {
    cout << "Hello from constructor\n";
  }

 private:
  int a;
  int b;
  void printfun() { cout << "a=" << a << endl << "b=" << b << endl; }
};
void f1() {  // ERROR HERE
  cout << "f1 start" << endl;
  classB tmp(3, 5);
  tmp.printfun();
  cout << "f1 end" << endl;
}

Example code (main.cpp):

// main.cpp
#include <iostream>

#include "file1.cpp"
using namespace std;

int main() {
  cout << "calling function" << endl;
  f1();
  cout << "exit from main" << endl;
  return 0;
}

This is what is happening in your circumstance. Because of #include in the file1.cpp file, both file1.cpp and main.cpp include a definition of f1(), and the linker doesn’t know which one to use in your program and complains about it.

The solution is to remove the CPP file containing the definition of f1() from main.cpp and instead include the declaration of f1() in a separate header file and include that in main.cpp. The compiler will have the declaration of f1() to deal with, and the linker will have only one definition of f1() from file1.cpp to rely on.

Example code (file1.h):

// file1.h
#include <iostream>

class classB {
  friend void f1();

 public:
  classB(int i = 1, int j = 2) : a(i), b(j) {
    std::cout << "Hello from constructor\n";
  }

 private:
  int a;
  int b;
  void printfun() { std::cout << "a=" << a << endl << "b=" << b << std::endl; }
};

Example code (file1.cpp):

// file1.cpp
#include "file1.h"
using namespace std;
void f1() {
  cout << "f1 start" << endl;
  classB tmp(5, 6);
  tmp.printfun();
  cout << "f1 end" << endl;
}

Example code (main.cpp):

// main.cpp
#include <iostream>

#include "file1.h"
using namespace std;

int main() {
  cout << "calling function" << endl;
  f1();
  cout << "exit from main" << endl;
  return 0;
}

Let us start with the header file file1.h. Header files contain the definitions of everything like function definitions, any struct or class definitions, or any constants definitions.

This .h extension tells the compiler that this file is not to be compiled. It is just like a text file and is readable by anyone.

This implies that the header file is a documenting file. If any programmer wants to use some functions in future times, they only need to check the prototype of the functions and don’t need to go into detail about function definitions.

Lastly, the template code should also be in the header file.

The CPP file now defines the function declared in the header file. This tells the compiler that this file is to be compiled and converted into a binary file.

Your code’s integrity will be protected, and no one else can modify it without your permission. This means that this separation of code also ensures the security of your code files.

One more reason behind this technique is portability. For example, you have written a binary search code that can be used later in many other programs.

If you have these separate files for functions, you can easily use these functions in any other project.

At last, the main file contains only the main function and includes the header file at the top. This file contains only the main function, which only calls all the functions and nothing else.

Muhammad Husnain avatar Muhammad Husnain avatar

Husnain is a professional Software Engineer and a researcher who loves to learn, build, write, and teach. Having worked various jobs in the IT industry, he especially enjoys finding ways to express complex ideas in simple ways through his content. In his free time, Husnain unwinds by thinking about tech fiction to solve problems around him.

LinkedIn

Related Article - C++ Error