2014-12-05 65 views
2

对于我的家庭作业,我应该编写一个结构来收集有关音乐专辑的信息。我能够轻松做到这一点。我的任务的第二部分是将我的结构变成一个类,并且我无法让我的代码编译。这是我的两个代码。将struct更改为C++类

结构代码:

#include <iostream> 
#include <vector> 
#include <string> 

using namespace std; 

struct Date 
{ 
    int month; 
    int day; 
    int year; 
}; 

struct Album 
{ 
    string name; 
    string artist; 
    vector <string> songs; 
    Date release; 
}; 

void initializeAlbum(Album& a); 
void initializeSongs(vector <string> & songs); 
void printAlbum(Album a); 

int main() 
{ 
    Album my_album; 
    initializeAlbum(my_album); 
    printAlbum(my_album); 
    return 0; 
} 


void initializeAlbum(Album& a) 
{ 
    cout << "Enter album name:" << endl; 
    getline(cin, a.name); 
    cout << "Enter artist name:" << endl; 
    getline(cin, a.artist); 
    cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl; 
    cin >> a.release.month; 
    cout << "Enter the release day:" << endl; 
    cin >> a.release.day; 
    cout << "Enter the release year:" << endl; 
    cin >> a.release.year; 

    initializeSongs(a.songs); 
} 

void initializeSongs(vector <string> & songs) 
{ 
    cout << "How many songs are in the album?" << endl; 
    int j = 0; 
    cin >> j; 
    string answer; 
    cout << "Enter each song name one at a time:" << endl; 
     for (int y = 0; y <= j; y++) 
     { 
      getline (cin, answer); 
      songs.push_back(answer); 
     } 
} 


void printAlbum(Album a) 
{ 
    cout << "The album name is " << a.name << endl; 
    cout << "The artist name is " << a.artist << endl; 
    cout << "The release date is " << a.release.day << "/" << a.release.month << "/" << a.release.year << endl; 
    cout << "The songs are:"; 
    for (int x = 0; x < a.songs.size(); x++) 
     cout << " " << a.songs[x] << endl; 
} 

类代码:

#include <iostream> 
#include <vector> 
#include <string> 

using namespace std; 
struct Date 
{ 
    int month; 
    int day; 
    int year; 
}; 


class Album 
{ 
    public: 
     void printAlbum(); 
     void initializeAlbum(); 
     string name; 
     string artist; 
     vector <string> songs; 
     Date release; 
    private: 
     void initializeSongs(); 
}; 
int main() 
{ 
    Album a; 
    a.initializeAlbum(); 
    a.printAlbum(); 
    return 0; 
} 

void Album::initializeAlbum() 
{ 
    cout << "Enter album name:" << endl; 
    getline(cin, name); 
    cout << "Enter artist name:" << endl; 
    getline(cin, artist); 
    cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl; 
    cin >> release.month; 
    cout << "Enter the release day:" << endl; 
    cin >> release.day; 
    cout << "Enter the release year:" << endl; 
    cin >> release.year; 
    initializeSongs(songs); 
} 

void Album::initializeSongs() 
{ 
    cout << "How many songs are in the album?" << endl; 
    int j = 0; 
    cin >> j; 
    string answer; 
    cout << "Enter each song name one at a time:" << endl; 
     for (int y = 0; y <= j; y++) 
     { 
      getline (cin, answer); 
      songs.push_back(answer); 
     } 
} 


void Album::printAlbum() 
{ 
    cout << "The album name is " << name << endl; 
    cout << "The artist name is " << artist << endl; 
    cout << "The release date is " << release.day << "/" << release.month << "/" << release.year << endl; 
    cout << "The songs are:"; 
    for (int x = 0; x < songs.size(); x++) 
     cout << " " << songs[x] << endl; 
} 
+8

当你尝试编译它时会得到什么错误? – EWit 2014-12-05 21:15:39

+2

如果你需要帮助弄清楚编译错误的含义(听起来像你在问什么),你至少应该在你的问题中包括这个。 – greatwolf 2014-12-05 21:20:28

+3

只是使用结构代码并公开所有内容:P – mukunda 2014-12-05 21:28:25

回答

1

也许这行是你的错误:initializeSongs(songs);void Album::initializeAlbum()功能?

遵循编译器消息。

1

这个函数零点参数:

void Album::initializeSongs() 
{ 
    cout << "How many songs are in the album?" << endl; 
    int j = 0; 
    cin >> j; 
    string answer; 
    cout << "Enter each song name one at a time:" << endl; 
     for (int y = 0; y <= j; y++) 
     { 
      getline (cin, answer); 
      songs.push_back(answer); 
     } 
} 

然而,你叫

initializeSongs(songs); 

您可以在类函数内访问您的会员所以只是把它想:

initializeSongs(); 

这编译得很好:

#include <iostream> 
#include <vector> 
#include <string> 

using namespace std; 
struct Date 
{ 
    int month; 
    int day; 
    int year; 
}; 


class Album 
{ 
    public: 
     void printAlbum(); 
     void initializeAlbum(); 
     string name; 
     string artist; 
     vector <string> songs; 
     Date release; 
    private: 
     void initializeSongs(); 
}; 
int main() 
{ 
    Album a; 
    a.initializeAlbum(); 
    a.printAlbum(); 
    return 0; 
} 

void Album::initializeAlbum() 
{ 
    cout << "Enter album name:" << endl; 
    getline(cin, name); 
    cout << "Enter artist name:" << endl; 
    getline(cin, artist); 
    cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl; 
    cin >> release.month; 
    cout << "Enter the release day:" << endl; 
    cin >> release.day; 
    cout << "Enter the release year:" << endl; 
    cin >> release.year; 
    initializeSongs(); 
} 

void Album::initializeSongs() 
{ 
    cout << "How many songs are in the album?" << endl; 
    int j = 0; 
    cin >> j; 
    string answer; 
    cout << "Enter each song name one at a time:" << endl; 
     for (int y = 0; y <= j; y++) 
     { 
      getline (cin, answer); 
      songs.push_back(answer); 
     } 
} 


void Album::printAlbum() 
{ 
    cout << "The album name is " << name << endl; 
    cout << "The artist name is " << artist << endl; 
    cout << "The release date is " << release.day << "/" << release.month << "/" << release.year << endl; 
    cout << "The songs are:"; 
    for (int x = 0; x < songs.size(); x++) 
     cout << " " << songs[x] << endl; 
} 
+0

我还想补充一点,在该类中还应该有构造函数以供良好实践。 – Poriferous 2014-12-05 22:42:29