0

Here's what I've got, the error is showing in the fourth to last line with the '='. I've tried to construct that line in several different ways, but they all give me errors. I think it is probably in the constructor itself. I have tried a bit of the stuff with '*' and '&' but I don't know how those work and nothing I tried seemed to work.

#include <string>
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
  • In the future, please post the full error message from your compiler since it contains a lot of useful info that makes it easier to troubleshoot. Commented Apr 22, 2022 at 0:59

1 Answer 1

1

The compiler is probably giving you a useful error message that says something like cannot convert from 'AudioCD *' to 'AudioCD'. The issue is that when you use the new keyword, you are actually allocating new memory for your object and you are getting a pointer to the object. The pointer has type AudioCD *. But there is no need to allocate memory: you already allocated enough memory when you defined your cdArray array above. Each element in the array is an actual AudioCD object, not just a pointer to one.

Try removing new from that line, so it becomes:

cdArray[i] = AudioCD(title, artists, releaseYear, genre, condition);

Then the types on the left and right side of the assignment will match.

Sign up to request clarification or add additional context in comments.

1 Comment

cdArray[i] = {title, artists, releaseYear, genre, condition}; is also possible; avoiding a bit of repetition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.