Undefined Reference to Class::Function() in C++

Muhammad Husnain Oct 12, 2023
  1. Types of Errors in C++
  2. Undefined Reference to a Class::Function() in C++
  3. Resolve Undefined Reference to a Class::Function() Due to No Function Definition in C++
  4. Resolve Undefined Reference to a Class::Function() Due to Not Linked Object Files in C++
  5. Resolve Undefined Reference to a Class::Function() Due to Not Compiled Dependent Files in C++
Undefined Reference to Class::Function() in C++

This tutorial briefly discusses one of the most common and equally critical errors in C++ programming (i.e., Undefined Reference to a Class::Function()).

First, we will briefly discuss different errors while coding in C++. Then, we will explain the causes and fixes for the undefined reference error.

Types of Errors in C++

The C++ codes may be subjected to errors or bugs for multiple reasons like any other programming language. These errors are broadly classified into the following error categories:

  • Syntax Errors are the errors that occur due to violations in the rules of C++ or any syntaxes.
  • Run-time Errors are the errors that occur when there is no programming issue syntactically but are detected at the time of execution and lead to a program crash.
  • Logical Errors occur when we are not getting our desired results or output, which means there are mistakes in the logic of our program.
  • Linker Errors are the type of errors when the program is compiled successfully and is trying to link some other objects with our main object file, and thus executable is not generated. For example, any wrong prototype of the function defined, any incorrect header file included, etc.

Undefined Reference to a Class::Function() in C++

This is the most frequently occurring error in C++ and is equally critical, especially for new programmers. This type of linker error can affect the running of the program.

These errors mainly occur when the program is compiled successfully and is at the linking phase trying to link other object files with the main object. There are some cases when the program gives some output, and this error occurs.

So, it is sometimes complicated to trace down and correct such errors.

Undefined Reference error occurs when we have used any reference to some class, function, or variable. The linker cannot find its definition in all the linked object files and libraries, thus generating the error Undefined Reference to a Class::Function().

There can be several causes of these errors, but we will discuss some of them here.

Resolve Undefined Reference to a Class::Function() Due to No Function Definition in C++

This can be the most straightforward reason for this error, that you have provided the prototype of the function but forgot to give its definition.

When you create a reference for that function, the linker will fail to find its definition, thus generating an error Undefined Reference to a Class::Function().

This is demonstrated in the example below:

#include <iostream>
class Square {
 public:
  double length;  // Length of a box
  double width;   // Breadth of a box
  double height;  // Height of a box

  double getArea(void);
};
int main() {
  Square sq1;
  sq1.getArea();
  return 0;
}

Output:

(.text+0x15): undefined reference to Square::getArea()
collect2.exe: error: ld returned 1 exit status

Resolve Undefined Reference to a Class::Function() Due to Not Linked Object Files in C++

When using a different file to define a class, we need to include that file in the main function file to be linked with the main object file when linking the program.

In the example below, we defined the Square class separately in another file. In the main file, we are referencing its object, so it will give an undefined reference error as the file is not linked.

Example:

#include <iostream>
using namespace std;

int main() {
  Square sq1;
  cout << sq1.getArea();
  return 0;
}

Example (Square.cpp):

class Square {
 public:
  double length;  // Length of a box
  double width;   // Breadth of a box
  double height;  // Height of a box
  double getArea(void) { return length * width; }
  void setLength(double l) { length = l; }
};

The solution to this problem is to link the Square.cpp file to the main file in the following code.

#include <iostream>

#include "square.cpp"
using namespace std;

int main() {
  Square sq1;
  cout << sq1.getArea();
  return 0;
}

Resolve Undefined Reference to a Class::Function() Due to Not Compiled Dependent Files in C++

This type of error also occurs when all of the program’s dependencies are not compiled successfully. For a linker to generate an executable successfully, it is necessary to compile all the dependencies before initiating the linking phase.

This is inevitable so that the linker may find all the dependent object files (i.e., .o files) easily. If any dependent object files go missing, the linker will generate an undefined reference error.

Note
If you use GNU’s g++ compiler, the Bash command g++ -c abc.cpp will generate a corresponding object file abc.o without invoking the linker.

Apart from the situations discussed above, there can be many other situations for the undefined reference error. The programmer must correctly define all identifiers (e.g., functions, variables, and objects) to avoid such problems.

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