Explanations of a few g++ compiler error messages

by Kevin Godby

I was going through some old notebooks tonight and found a page where I’d noted some of the error messages that g++ gave me during some early C++ coursework. I took the time to write nice explanations of the error messages: what they mean and how to fix them. I figured that someone else may find them useful, so I’ll post them here. If you have good explanations of other g++ compiler error messages that you’ve encountered, feel free to leave a comment.

error: cannot declare member function ā€˜static void C::f()ā€™ to have static linkage

You have put static not only on the member function declaration, but on the definition too. Remove static from the definition.

class C
    static void f();

void C::f() // ← there should be no static here
    // ...

error: static member function ‘static void C::f()’ cannot have cv-qualifier

You can’t have a const static member function. Remove the const qualifier from the static member function.

The const qualifier means that the implicit this* will not be modified by the member function. However, this* is not passed to static member functions—only non-static member functions. Therefore, there is no this* to be declared const.

undefined reference to vtable for C::f()

Check for the following:

  • You declared a virtual method in a base class but did not implement it. Also, make sure your build system is including the .o or .cpp file that defines the method.
  • If you have a static member variable, you must define it in your .cpp file too:
    // c.h
    class C
        static std::vector<std::string> databases;
    // c.cpp
    #include "c.h"
    std::vector<std::string> C::databases; // ← define it in your .cpp file as well!

B is an inaccessible base of C

B is a base class for C. In class A, you’re trying to access B but you’ve likely forgotten to mark the inheritance public:

// c.h

class B { }; // a base class

class C : B // ← forgot to specify public B!
        static B* create(); // trying to return the parent class type, 
                            // but it can't be accessed (it's private by default)

Also, the public qualifier isn’t distributive. So:

class C : public B1, B2 { }; // B2 is private here

is not the same as:

class C : public B1, public B2 { };