Reputation: 1
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);
}```
Upvotes: 0
Views: 46
Reputation: 87541
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.
Upvotes: 1