Single class has a Class Redefinition Error

后端 未结 4 1130
眼角桃花
眼角桃花 2021-01-04 19:00

I\'m new to C++, and I\'m having a problem with my class definitions in a header file. The code for the header file (Student.h) is:

#include 
u         


        
4条回答
  •  孤独总比滥情好
    2021-01-04 19:46

    You need header guards on that header file. It is presumably being included twice.

    Modify the header, adding these lines to the beginning and end.

    #ifndef STUDENT_H
    #define STUDENT_H
    
    // Put the entire contents of your header here...
    
    #endif
    

    The define doesn't need to be STUDENT_H... it just needs to be unique.

    With these directives added, the compiler will ignore all contents of the header file if it has already been parsed.

    Alternatively, while it is not standard C++, all major compilers will allow you to put a single

    #pragma once
    

    as the first line of the header to prevent it from being parsed multiple times.

提交回复
热议问题