C ++包含和重新定义类错误 [英] C++ include and redefinition of class error

查看:103
本文介绍了C ++包含和重新定义类错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编程一个程序,该程序可以根据不同的歌曲进行搜索 参数. 在我的系统中,有两种类型的歌曲:歌词和instrumetal. 因为我需要将它们都放在1个向量中,所以我有一个歌曲课, 歌词InstrumentalSong子类.

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.

所以我有一个Song.h文件:

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

都包含Song.h,在这两个类中都定义了类 仅在头文件中.

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"

(显然还有更多的cpp库),我得到以下编译错误:

(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
       ^

何时:

  • lines InstrumentalSong.h:16:0和LyricsSong.h:15:0是我在哪里 包括"Song.h"
  • 我在
  • lines songsParser.cpp:25和songsParser.cpp:26行中 包括InstrumentalSong.h和LyricsSong.h
  • line Song.h:6:7:是Song.h的定义(在这里 如上图所示).
  • 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).

我该怎么办? P.S.我从不导入任何cpp文件,仅导入头文件.

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

推荐答案

您必须告诉预处理器仅包含一次头文件.您可以通过在所有*.h文件顶部添加#pragma once来实现:

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 ++包含和重新定义类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆