给出错误“未在此范围内声明”的结构向量

发布于 2024-12-13 18:48:41 字数 741 浏览 1 评论 0原文

我有一个声明如下的结构:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪异←思 2024-12-20 18:48:41

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.

还不是爱你 2024-12-20 18:48:41

不仅是您的主文件,声明 Playlist 的文件也应该 #include Song 所在的文件。

Not only your main file, but the file where Playlist is declared should also #include the file where Song is in.

∝单色的世界 2024-12-20 18:48:41

Song 的定义必须位于 Playlist 定义中使用的定义之前。由于它们位于不同的标头中,因此您应该确保 Playlist 的标头包含 Song 的标头,并且两者都有适当的标头防护。

The definition of Song has to come before its used in the definition of Playlist. Since they are in different headers, you should make sure that the header for Playlist includes that of Song, and both have proper header guards.

川水往事 2024-12-20 18:48:41

您的标头以循环方式相互包含。这是无用且不必要的。为什么您的 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 including playlist.h? Remove #include "playlist.h" from song.h and that should fix the error.

预谋 2024-12-20 18:48:41

您可以通过放置 struct Song; 在标头中原型 Song,然后将标头包含在 .c/.cpp 文件中。这样做的好处是编译时间更快! :D

递归包含与包含防护一起工作得很好,只要你正确地安排它们。我总是尝试在 .h 文件中包含最少量的标头,将它们留给源文件。

另外,我在您的代码中没有看到#endif。现在我假设你的代码实际上有它;)

You could prototype Song in your header by putting struct Song; and just include the header in your .c/.cpp file. This has the bonus of faster compile times! :D

Recursive 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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文