C++ include and redefinition of class error

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 put both of them in 1 vector, I have a song class and a LyricsSong & InstrumentalSong subclasses.

So I there is a Song.h file:

#include <stdio.h>
#include <iostream>
#include <string>


class Song
{
public:
    std::string title;
    virtual void print();
    virtual void printSong(std::string query);
};

and there are the instrumental and lyrics subclasses, which are defined this way:

class LyricsSong : public Song
class InstrumentalSong : public Song

both of the include Song.h, and in both of them the class is defines only in the header file.

when I try to run another file which use the both subclasses, and includes:

#include "LyricsSong.h"
#include "InstrumentalSong.h"

(and obviously more cpp libraries), i get the following compilation error:

In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/InstrumentalSong.h:16:0,
                 from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:26:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: redefinition of 'class Song'
 class Song
       ^
In file included from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/LyricsSong.h:15:0,
                 from /cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/songsParser.cpp:25:
/cygdrive/c/Users/Username/Documents/C++ Workshop/ex2/ex2_code/Song.h:6:7: error: previous definition of 'class Song'
 class Song
       ^

when:

  • lines InstrumentalSong.h:16:0 and LyricsSong.h:15:0 are where i include "Song.h"
  • lines songsParser.cpp:25 and songsParser.cpp:26 are where i include InstrumentalSong.h and LyricsSong.h
  • line Song.h:6:7: is the defination of Song.h (where it's say's class Song, as showed above).

What should I do? P.S. I do not import any cpp file ever, only header files.

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.

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) C (/ s iː /, as in the letter c) is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system.By design, C provides constructs that map efficiently to typical machine instructions.It has found lasting use in applications previously coded in assembly language.

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 

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 "C" comes from the same letter as "G". The Semites named it gimel.The sign is possibly adapted from an Egyptian hieroglyph for a staff sling, which may have been the meaning of the name gimel.Another possibility is that it depicted a camel, the Semitic name for which was gamal.Barry B. Powell, a specialist in the history of writing, states "It is hard to imagine how gimel = "camel" can be

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).

C Tutorial, Computerphile. Computerphile. 1.74M subscribers. "C" is one of the most widely used Duration: 8:26 Posted: Aug 18, 2015 Stock analysis for Citigroup Inc (C:New York) including stock price, stock chart, company news, key statistics, fundamentals and company profile.

Learn C, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone� C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.

Learn C Programming, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.. When not overloaded, for the operators &&, ||, and , (the comma operator), there is a sequence point after the evaluation of the first operand.

C Programming Language, C is a powerful general-purpose programming language. Our C tutorials will guide you to learn C programming one step at a time with the help of examples. As well as C and Simula's influences, other languages also influenced this new language, including ALGOL 68, Ada, CLU and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining and default arguments.

Comments
  • You are missing include guards: stackoverflow.com/questions/1653958/…
  • my problem is too many definitions, not missing ones, and i don't know how to use them in this kind of case.
  • I wasn't talking about missing definitons, but missing include guards. Did you even read the link?
  • #pragma once is not standard, and it can not work in some situations. It was explicitly not standardized because of those (not portably solvable) problems. See here for some issues.
  • But it actually solved my problem. I spent around an hour searching for a solution and this^ solved it. But why does it have that one drawback?
  • In most compilers you can use more efficent #pragma once also
  • @DawidPi I don't like to use tools/features which is non-standard and don't suggest anyone to do so.
  • Most importantly, he needs to put an include guard into Song.h. Putting it into LyricsSong.h and InstrumentalSong.h is good practice, but not enough.
  • I uderstand this in fact, I also prefer standard solutions but #pragma once is available in every modern compiler. msvs,gcc,clang or whatever. And if you have lot's of files it can reduce compile time significantly
  • At least for gcc #pragma once is probably no more efficient than the standard include guards, see here. Other compilers probably have similar optimizations.