[c++] What is an undefined reference/unresolved external symbol error and how do I fix it?

Class members:

A pure virtual destructor needs an implementation.

Declaring a destructor pure still requires you to define it (unlike a regular function):

struct X
{
    virtual ~X() = 0;
};
struct Y : X
{
    ~Y() {}
};
int main()
{
    Y y;
}
//X::~X(){} //uncomment this line for successful definition

This happens because base class destructors are called when the object is destroyed implicitly, so a definition is required.

virtual methods must either be implemented or defined as pure.

This is similar to non-virtual methods with no definition, with the added reasoning that the pure declaration generates a dummy vtable and you might get the linker error without using the function:

struct X
{
    virtual void foo();
};
struct Y : X
{
   void foo() {}
};
int main()
{
   Y y; //linker error although there was no call to X::foo
}

For this to work, declare X::foo() as pure:

struct X
{
    virtual void foo() = 0;
};

Non-virtual class members

Some members need to be defined even if not used explicitly:

struct A
{ 
    ~A();
};

The following would yield the error:

A a;      //destructor undefined

The implementation can be inline, in the class definition itself:

struct A
{ 
    ~A() {}
};

or outside:

A::~A() {}

If the implementation is outside the class definition, but in a header, the methods have to be marked as inline to prevent a multiple definition.

All used member methods need to be defined if used.

A common mistake is forgetting to qualify the name:

struct A
{
   void foo();
};

void foo() {}

int main()
{
   A a;
   a.foo();
}

The definition should be

void A::foo() {}

static data members must be defined outside the class in a single translation unit:

struct X
{
    static int x;
};
int main()
{
    int x = X::x;
}
//int X::x; //uncomment this line to define X::x

An initializer can be provided for a static const data member of integral or enumeration type within the class definition; however, odr-use of this member will still require a namespace scope definition as described above. C++11 allows initialization inside the class for all static const data members.

Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to linker-errors

Duplicate Symbols for Architecture arm64 Multiple definition of ... linker error What is an undefined reference/unresolved external symbol error and how do I fix it? Why am I getting "undefined reference to sqrt" error even though I include math.h header? What causes the error "undefined reference to (some function)"? Undefined reference to 'vtable for xxx' C error: undefined reference to function, but it IS defined Undefined reference to sqrt (or other mathematical functions) Undefined reference to `sin` Linker error: "linker input file unused because linking not done", undefined reference to a function in that file

Examples related to undefined-reference

DSO missing from command line gcc: undefined reference to libpthread.so.0: error adding symbols: DSO missing from command line What is an undefined reference/unresolved external symbol error and how do I fix it? Undefined reference to `pow' and `floor' C error: undefined reference to function, but it IS defined Undefined Reference to Undefined reference to sqrt (or other mathematical functions) Undefined reference to `sin` C++ undefined reference to defined function

Examples related to c++-faq

What are the new features in C++17? Why should I use a pointer rather than the object itself? Why is enum class preferred over plain enum? gcc/g++: "No such file or directory" What is an undefined reference/unresolved external symbol error and how do I fix it? When is std::weak_ptr useful? What XML parser should I use in C++? What is a lambda expression in C++11? Why should C++ programmers minimize use of 'new'? Iterator invalidation rules

Examples related to unresolved-external

unresolved external symbol __imp__fprintf and __imp____iob_func, SDL2 What is an undefined reference/unresolved external symbol error and how do I fix it? Unresolved external symbol in object files