返回

C++以数组作为参数创建对象

发布时间:2022-04-25 10:39:58 333

这就是我所得到的,错误显示在倒数第四行,带有“=”。我试图以几种不同的方式构建这条线,但它们都给了我错误。我认为它可能在构造函数本身中。我已经尝试了一些带有“*”和“&”的东西,但我不知道它们是如何工作的,而且我尝试过的任何东西似乎都不起作用。

#include 
using namespace std;

class AudioCD{
  public:
  string cdTitle;
  string artists[4];
  int releaseYear;
  string genre;
  float condition;

  AudioCD(){
    cdTitle = "";
    for(int i = 0; i<4; i++){
    artists[i] = "";
    }
    releaseYear = 1980;
    genre = "";
    condition = 0.0;
  }

  AudioCD(string newCDTitle, string newArtists[4], int newReleaseYear, string newGenre, float newCondition){
    cdTitle = newCDTitle;
    for(int i; i < 4; i++){
      artists[i] = newArtists[i];
    }
    if(newReleaseYear < 1980){
      releaseYear = 1980;
    }
    else{
      releaseYear = newReleaseYear;
    }
    genre = newGenre;
    if(newCondition > 5.0 || newCondition < 0.0){
      condition = 0.0;
    }
    else{
      condition = newCondition;
    }
  }

  void getCDInfo(int cdNum){
    cout << cdNum << ". " << cdTitle << ", " << releaseYear << endl;
    for(int i = 0; i < 4; i++){
      if(artists[i].compare("")!=0){
        cout << "Artist (#" << i << "): " << artists[i] << endl;
      }
    }
    cout << "Genre: " << genre << endl;
    cout << "Condition: " << condition << endl;
  }
};

int main() {
  int arraySize;
  string title;
  string artists[4];
  int releaseYear;
  string genre;
  float condition;

  cout << "[Rate Audio CD Collection]" << endl;
  cout << "How many CDs do you have lying around your car? ";
  cin >> arraySize;
  AudioCD cdArray[arraySize];
  for(int i = 0; i < arraySize; i++){
    cout << "Enter Title: ";
    cin >> title;
    cout << "Enter Artists (type -1 when finished)" << endl;
    for(int i = 0; i < 4; i++){
      string tempArtist;
      cin >> tempArtist;
      if(tempArtist.compare("-1")!=0){
        artists[i]=tempArtist;
      }
      else{
        break;
      }
    }
    cout << "Enter Genre: ";
    cin >> genre;
    cout << "Enter Release Year: ";
    cin >> releaseYear;
    cout << "Enter Condition: ";
    cin >> condition;

    cdArray[i] = new AudioCD(title, artists, releaseYear, genre, condition);
  }
  cdArray[1].getCDInfo(1);
}```
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像