C++ C++;:结构中的成员函数
因此,我有一个结构,它接受四个不同参数(名称、艺术家、大小和添加的日期)的条目,但是我有另一个结构,本质上是条目结构的库,我想在库结构中创建一个insert memeber函数,该函数接受一个参数,该参数是放置在库中的条目 在页眉中.hC++ C++;:结构中的成员函数,c++,function,struct,C++,Function,Struct,因此,我有一个结构,它接受四个不同参数(名称、艺术家、大小和添加的日期)的条目,但是我有另一个结构,本质上是条目结构的库,我想在库结构中创建一个insert memeber函数,该函数接受一个参数,该参数是放置在库中的条目 在页眉中.h struct MusicEntry{ string name, artist, date_added; long size; MusicEntry() = default; MusicEntry(string name_str,
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
};
在函数.cpp中
MusicEntry MusicLibrary::insert(){
//some code
}
每首歌曲都有一个唯一的ID,这是通过insert成员函数传递的内容。我假设您希望MusicLibrary包含MusicEntry的所有实例,因此您应该查看诸如std::vector之类的通用容器 应使用引用(&)或指针(*)将MusicCentry传递到MusicLibrary 或
您正在寻找
void insert(const MusicEntry&)代码>?你说你想要一个带参数的函数,但是你的代码显示了一个不带参数的函数。我不知道如何编写它,所以我现在就发布了代码中的内容。
MusicEntry* MusicLibrary::insert(const MusicEntry* myEntry){
//some code
}
MusicEntry& MusicLibrary::insert(const MusicEntry& myEntry){
//some code
}