Common compilation problems
Since C++ compilation error messages are often cryptic when you’re new to the language, we’ve put together a table of common error messages and their likely causes.
Message Possible Causes
error: expected class-name before ‘int’
int main()
^~~
Something declared before int main() {…} is likely missing a semi-colon. If there’s nothing obvious but you’re including header files before main, check your header files.
Expected unqualified-Id before ‘&’ token You probably have a method such as:
const std::string Tag::&getType() const{}
but the & needs to go before the Tag::.
error: no matching function for call to
‘Cell::attach(__gnu_cxx::__alloc_traits
c.attach(theGrid[a][b]);
Cell::attach() takes a pointer, but you’re passing in an object (by reference).
error: ‘Position& operator+=(const Position&)’ must take exactly two arguments
operator+= takes two parameters, since it’s intended to be used as a += b;, which is equivalent to operator+=(Position a, Position b);. You likely meant to make it a method in the Position class, though, so you need to put Position:: in front of operator+= when you implement it.
prototype for ‘Info Cell::getInfo()’ does not match any in class Cell Method signatures don’t match. Compare to the list of prototypes that the compiler tells you it found. You’re probably missing a const somewhere.
undefined reference to `Echo::Echo()’ There are several possible causes, such as not linking in the .o file for the Echo class, or not including the Echo class header file (a forward declaration for Echo isn’t enough).
undefined reference to `vtable for X` The class X either has or inherits a pure virtual method. An object of the class cannot be created without the virtual method being implemented.
Segmentation fault Several possible causes: trying to delete an object that has already been deleted; trying to access freed memory; trying dereferencing nullptr or a pointer that was never initialized.