Preventing class redefinition errors in C++

I have just started learning C++ and have been getting a lot of these errors. I finally found a solution to this after spending some time on the internet.

Say for instance you have a class called “Animal” and g++ complains that this class is being redefined. Just add the following two lines at the very top of the Animal.h file:

#ifndef ANIMAL_H
#define ANIMAL_H

and add the following at the very bottom of the file:


So, if your class is called SomeClass, then it will be SOMECLASS_H instead of ANIMAL_H. You should have these in every header file. It prevents that header file from being redefined more than once.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.