C++ include and redefinition of class error

前端 未结 3 2038
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 19:36

I am currently programming a program which searches song according to diffrent parameters. In my system there are 2 types of songs: lyric and instrumetal. Since i need to p

相关标签:
3条回答
  • 2021-01-14 20:08

    As already answered, I would also use #pragma once, it is more convenient and clean. But be aware that it is not a C++ standard, so it can be a problem if you have to use different compilers (although it is a wide-spread extension).

    0 讨论(0)
  • 2021-01-14 20:14

    You have to tell preprocessor to include your header files only once. You can achieve by adding #pragma once on the top of all *.h files:

    #pragma once
    
    //Your header file's code
    

    It is also a good practice to always begin header files with this line.

    0 讨论(0)
  • 2021-01-14 20:17

    They both include 'Song.h' file and preprocessor takes the file content twice. You need to write 'LyricsSong.h' and 'InstrumentalSong.h' file contents inside #ifndef #define and #endif directives. Like this

    #ifndef LYRICS_SONG_H
    #define LYRICS_SONG_H
    
    your code goes here.
    ...
    
    #endif 
    
    0 讨论(0)
提交回复
热议问题