Names of Objects question

So I have written this program to overload the == operator and compare objects of the MP3 class. I need to generate 1000 objects of this class for comparison purposes(which isn't what the question is about so I won't go there) using a random string function to create each name, how would i go about doing this? Here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class MP3{
public:
	void setartist(string art);
	string getartist();
	void setsongname(string song);
	string getsongname();
	void setalbum(string alb);
	string getalbum();
	void setgenre(string gen);
	string getgenre();
	void settracknumber(int track);
	int gettracknumber();
	void setbitrate(int bit);
	int getbitrate();
	void settracklength(int length);
	int gettracklength();
	void setfilesize(float size);
	float getfilesize();

	
	friend bool operator==(MP3 left, MP3 right);
	//friend bool operator==(const MP3& left, const MP3& right);
private:
	string artist;
	string songname;
	string album;
	string genre;
	int tracknumber;
	int bitrate;
	int tracklength;
	float filesize;
};
//bool operator==(MP3 left, MP3 right);
string Getrandomstring( int size);
/*bool operator==(MP3 left, MP3 right){
if( (left.getartist()).compare(right.getartist()) ==0 && (left.getsongname()).compare(right.getsongname()) ==0 && (left.getalbum()).compare(right.getalbum()) ==0 && (left.getgenre()).compare(right.getgenre())==0 && left.gettracknumber() == right.gettracknumber()&& left.getbitrate() == right.getbitrate()&& left.gettracklength() == right.gettracklength() && left.getfilesize() == right.getfilesize()){
		return true;
	}else
		return false;
}*/
bool operator==(MP3 left, MP3 right){
	if(left.artist == right.artist && left.songname == right.songname && left.album == right.album && left.genre == right.genre && left.tracknumber == right.tracknumber&& left.bitrate == right.bitrate&& left.tracklength == right.tracklength && left.filesize == right.filesize){
		return true;
	}else
		return false;
}
/*bool operator==(const MP3& left, const MP3& right){
	if(left.artist == right.artist && left.songname == right.songname && left.album == right.album && left.genre == right.genre && left.tracknumber == right.tracknumber&& left.bitrate == right.bitrate&& left.tracklength == right.tracklength && left.filesize == right.filesize){
		return true;
	}else
		return false;
}*/
void MP3::setartist(string art){
	if(art.length()<5||art.length()>12){
		cout<<"This is not a valid artist name."<<endl;
	}else
		artist = art;
}
string MP3::getartist(){
	return artist;
}
void MP3::setsongname(string song){
	if(song.length()<5||song.length()>12){
		cout<<"This is not a valid song name."<<endl;
	}else
		songname = song;
}
string MP3::getsongname(){
	return songname;
}
void MP3::setalbum(string alb){
	if(alb.length()<5||alb.length()>8){
		cout<<"This is not a valid album name."<<endl;
	}else
		album = alb;
}
string MP3::getalbum(){
	return album;
}
void MP3::setgenre(string gen){
	string types[10] = {"Grunge", "Pirate Metal", "Swedish Viking Pop", "Mathcore", "Easy Listening", "Indie",  "Classic Rock", "Leprechaun Music", "Zelda Themes", "Eurodance"};
		if (gen != types[0]&&gen != types[1]&&gen != types[2]&&gen != types[3]&&gen != types[4]&&gen != types[5]&&gen != types[6]&&gen != types[7]&&gen != types[8]&&gen != types[9]){
			cout<<"That is an invalid genre."<<endl;
		}else
			genre = gen;
}
string MP3::getgenre(){
	return genre;
}
void MP3::settracknumber(int track){
	if(track<0 || track>12){
		cout<<"This is not a valid track number."<<endl;
	}else
		tracknumber = track;
}
int MP3::gettracknumber(){
	return tracknumber;
}
void MP3::setbitrate(int bit){
	if(bit != 64&& bit != 128&& bit !=256&& bit !=512&& bit !=1024){
		cout<<"That is not a valid bitrate."<<endl;
	}else
		bitrate = bit;
}
int MP3::getbitrate(){
	return bitrate;
}
void MP3::settracklength(int length){
	if(length<20||length>240){
		cout<<"This is not a valid track length."<<endl;
	}else
		tracklength = length;
}
int MP3::gettracklength(){
	return tracklength;
}
void MP3::setfilesize(float size){
	if(size<1||size>3){
		cout<<"This is not a valid filesize."<<endl;
	}else
		filesize = size;
}
float MP3::getfilesize(){
	return filesize;
}
string Getrandomstring(int size){

	static const char valid_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz";
	stringstream stream;
	for (int i = 0; i<size; i++)
		stream<<valid_chars [rand()% (sizeof(valid_chars) -1)];

	return stream.str();
}
int main(){
	MP3 Itunes;//example object
	Itunes.setartist("Nirvana");
	Itunes.setalbum("InUtero");
	Itunes.setbitrate(256);
	Itunes.setgenre("Grunge");
	Itunes.setsongname("AllApologies");
	Itunes.setfilesize(2.4);
	Itunes.settracklength(220);
	Itunes.settracknumber(12);

	string names[1000];

	for( int i = 0; i<1000; i++){

	   names[i] =  Getrandomstring(7);
	}
	
	for(int i = 0;i<1000; i++){
		
	   MP3 names[i];
           names[i].setartist(Getrandomstring(7));
		
	}
	
	return 0;
}


The main part I need looked at is
1
2
3
4
5
6
7
8
9
10
11
12
13
	string names[1000];

	for( int i = 0; i<1000; i++){

	   names[i] =  Getrandomstring(7);
	}
	
	for(int i = 0;i<1000; i++){
		
	   MP3 names[i];
           names[i].setartist(Getrandomstring(7));
		
	}


As of right now I get a few errors

expected constant expression
cannot allocate an array of constant size 0
'names': unknown size

all of these are at the MP3 names[i]; line. Any help is appreciated, Thanks!
Last edited on
I didn't read your whole code. It was too long.
Any way, as for your error, you need to provide constant expression to your array declarations.
1
2
MP3 names[i]; won't do, the compiler expects something like
MP3 names[100]; 

.
If you want to create your array *your way* then you are gonna need Dynamic Memory Allocation. example
1
2
3
int i = 0;
MP3 names[i]; //invalid
MP3 *names = new MP3[100]; //valid 
Thank you!! I was thinking about this in completely the wrong direction, works fine now! This is very much appreciated and sorry for the long block of code, I just thought I should give the whole thing in case somebody needed to see it to help.
Topic archived. No new replies allowed.