C++ class redefinition error - Help me understand headers and linking

后端 未结 3 517
梦毁少年i
梦毁少年i 2021-01-12 13:54

I started writing a simple interpreter in C++ with a class structure that I will describe below, but I quit and rewrote the thing in Java because headers were giving me a ha

3条回答
  •  不思量自难忘°
    2021-01-12 14:30

    Why can I #include a million times, even one after the other, but I can only include my header once?

    It is probably because your header doesn't have an include guard.

    // printer.h file
    #ifndef PRINTER_H_
    #define PRINTER_H_
    
     // printer.h code goes here
    
    #endif
    

    Note that it is best practice to chose longer names for the include guard defines, to minimise the chance that two different headers might have the same one.

提交回复
热议问题