What are undefined reference/unresolved external symbol errors in C++?

C++Server Side ProgrammingProgramming

As the name suggests, a symbol you declared was not defined by you. This may occur due to many cases. Let's have a look at three of them −

  • You forgot to define the declared name. For example, you declared a function in a file and used it somewhere. But you did not provide its definition. Code −

#include<iostream>
void foo();
int main() {
   foo(); // Declared but not defined
}
  • You defined it but did not use the qualified name. Say you created a class with a method and defined that method but forgot using scope resolution to link that function to that definition and used that function somewhere. For example,

#include<iostream>
class A {
   void foo();
};
void foo() { } // should have used A::foo
int main() {
   A a;
   a.foo(); // Not defined!
}
  • You did not include dependencies in incorrect order. The linker tries to link in the order you specify to it. So in case you use a method before its definition, it will likely give this error.

Creating an exhaustive list of conditions that can cause this error is not possible. But you can refer to this post for a very well-curated list of likely reasons − https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix

raja
Published on 27-Feb-2018 14:09:47
Advertisements