【发布时间】:2015-11-29 09:23:25
【问题描述】:
我目前正在编写一个根据不同搜索歌曲的程序 参数。 在我的系统中有两种类型的歌曲:抒情和乐器。 因为我需要把它们都放在一个向量中,所以我有一个歌曲类和 一个 LyricsSong 和 InstrumentalSong 子类。
所以我有一个 Song.h 文件:
#include <stdio.h>
#include <iostream>
#include <string>
class Song
{
public:
std::string title;
virtual void print();
virtual void printSong(std::string query);
};
还有乐器和歌词子类,它们是这样定义的:
class LyricsSong : public Song
class InstrumentalSong : public Song
都包含 Song.h,并且在它们中都定义了类 仅在头文件中。
当我尝试运行另一个使用这两个子类的文件时, 并包括:
#include "LyricsSong.h"
#include "InstrumentalSong.h"
(显然还有更多 cpp 库),我得到以下编译错误:
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
^
何时:
- InstrumentalSong.h:16:0 和 LyricsSong.h:15:0 行是 i 包括“Song.h”
- 行songsParser.cpp:25 和songsParser.cpp:26 是我的位置 包括 InstrumentalSong.h 和 LyricsSong.h
- line Song.h:6:7: 是 Song.h 的定义(据说是 歌曲类,如上所示)。
我该怎么办? 附言我从不导入任何 cpp 文件,只导入头文件。
【问题讨论】:
-
我的问题是定义太多,没有漏掉,而且我不知道在这种情况下如何使用它们。
-
我说的不是缺少定义,而是缺少包含守卫。你读过链接吗?
标签: c++ inheritance include polymorphism multiple-inheritance