0

所以我有一个结构,它接受四个不同参数(名称、艺术家、大小和添加日期)的条目,但是我有另一个结构,它本质上是条目结构的库,我想在其中创建一个插入成员函数采用一个参数的库结构,该参数是要放置在库中的条目。

在 HEADER.h 中

struct MusicEntry{
    string name, artist, date_added;
    long size;

    MusicEntry() = default;
    MusicEntry(string name_str, string artist_str, long size_int, string date_added_str) : 
    name(name_str), artist(artist_str), size(size_int), date_added(date_added_str) {};
    MusicEntry to_string();
};

struct MusicLibrary{

    MusicLibrary(string) {};
    MusicLibrary to_string();
    MusicEntry insert(); //not sure how this should be passed with MusicEntry

};

在 FUNCTION.cpp 中

MusicEntry MusicLibrary::insert(){
     //some code
}

每首歌曲都有一个唯一的 ID,这就是通过插入成员函数传递的内容。

4

1 回答 1

0

我假设您希望 MusicLibrary 包含 MusicEntry 的所有实例,因此您应该查看通用容器,例如 std::vector。

http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#VECTOR

应该使用引用 (&) 或指针 (*) 将 MusicEntry 传递到 MusicLibrary。

MusicEntry* MusicLibrary::insert(const MusicEntry* myEntry){
     //some code
}

或者

MusicEntry& MusicLibrary::insert(const MusicEntry& myEntry){
     //some code
}
于 2013-10-31T01:09:23.023 回答