给出错误“未在此范围内声明”的结构向量
我有一个声明如下的结构:
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
#include <vector>
#include "playlistitem.h"
#include "song.h"
#include "time.h"
struct Playlist {
std::vector<Song> songs;
Time cdTotalTime;
int totalTime;
};
和在另一个文件中声明的结构 Song:
#ifndef SONG_H
#define SONG_H
#include "playlist.h"
#include "time.h"
struct Song {
std::string title;
std::string artist;
std::string album;
int track;
Time length;
};
我在标头中都有两个结构定义,并且两者都应为 #included。
当我编译时出现错误
std:vector<Song> songs;
错误“歌曲”未在此范围内声明
我缺少什么?
I have a struct declared as follows:
#ifndef PLAYLIST_H
#define PLAYLIST_H
#include <iostream>
#include <string>
#include <vector>
#include "playlistitem.h"
#include "song.h"
#include "time.h"
struct Playlist {
std::vector<Song> songs;
Time cdTotalTime;
int totalTime;
};
and struct Song declared in another file:
#ifndef SONG_H
#define SONG_H
#include "playlist.h"
#include "time.h"
struct Song {
std::string title;
std::string artist;
std::string album;
int track;
Time length;
};
I have both struct definitions in headers, and both are #included as they should be.
When I compile I get an error at
std:vector<Song> songs;
error 'Song' was not declared in this scope
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
playlist.h 包含 Song.h
Song.h 不应包含 playlist.h
标头防护可防止无限递归,它们不会修复循环依赖关系。
目前song.h确实包含playlist.h。然后,当 playlist.h 包含 Song.h 时,没有任何反应(因为头保护),并且
Song
未定义。因此 playlist.h 会产生错误。playlist.h includes song.h
song.h should NOT include playlist.h
Header guards prevent infinite recursion, they don't fix circular dependencies.
Currently song.h does include playlist.h. Then when playlist.h includes song.h, nothing happens (because of the header guard), and
Song
is not defined. So playlist.h produces errors.不仅是您的主文件,声明
Playlist
的文件也应该 #includeSong
所在的文件。Not only your main file, but the file where
Playlist
is declared should also #include the file whereSong
is in.Song
的定义必须位于Playlist
定义中使用的定义之前。由于它们位于不同的标头中,因此您应该确保Playlist
的标头包含Song
的标头,并且两者都有适当的标头防护。The definition of
Song
has to come before its used in the definition ofPlaylist
. Since they are in different headers, you should make sure that the header forPlaylist
includes that ofSong
, and both have proper header guards.您的标头以循环方式相互包含。这是无用且不必要的。为什么您的
song.h
包含playlist.h
?从song.h
中删除#include "playlist.h"
即可修复该错误。Your headers include each other in circular fashion. This is useless and unnecessary. Why is your
song.h
includingplaylist.h
? Remove#include "playlist.h"
fromsong.h
and that should fix the error.您可以通过放置
struct Song;
在标头中原型Song
,然后将标头包含在 .c/.cpp 文件中。这样做的好处是编译时间更快! :D递归包含与包含防护一起工作得很好,只要你正确地安排它们。我总是尝试在 .h 文件中包含最少量的标头,将它们留给源文件。
另外,我在您的代码中没有看到#endif。现在我假设你的代码实际上有它;)
You could prototype
Song
in your header by puttingstruct Song;
and just include the header in your .c/.cpp file. This has the bonus of faster compile times! :DRecursive includes work fine with include guards, as long as you arrange them properly. I always try and include the least amount of headers in .h files, leaving them for the source files.
Also, I don't see a #endif on your code. Right now I'm assuming your code actually has it ;)