Constructor Assistance

I have to change my code into using member functions and constructors but can only change 2 files, yet keep getting errors. I'm unsure of how to properly use the functions from the other file in the songcollection.cpp file. It keeps saying "error: '((SongCollection*)this)->SongCollection::item' does not have class type|." Any tips would be appreciated

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  songcollection.cpp

#include <iostream>
#include <string>
#include "songcollection.h"


using namespace std;

SongCollection::SongCollection()
{
    numSongs = 0;
    totalTime = time(0,0,0);
}

/**
* Add a single song to a collection.
*/
void SongCollection::addSong (
Song song)
{
int n = numSongs;
songs[n]=song;

++numSongs;
add(totalTime, songs[n].length);
}


/**
* Add into one collection all songs from another collection that
* match a given artist and/or title.
*/
void SongCollection::addMatchesFor (
SongCollection& toCollection,
std::string artist,
std::string title)
{
for (int i = 0; i < numSongs; ++i)
{
if (item.matches(songs))
{
addSong (songs[i]); }
}
}


void SongCollection::printAll (std::ostream& out)
{ for (int i = 0; i < numSongs; ++i)
{
print (out, songs[i]);
out << endl;
}
out << "Total: ";
print(out, totalTime);
out << endl;
}


/**
* Attempt to read a song from an input stream, adding it
* to the collection if possible,
*/
bool SongCollection::readAll (std::istream& in)
{
Song song;
while (item.read(in))
{
addSong(song);
}
return in;
}

songcollection.h
#ifndef SONGCOLLECTION_H
#define SONGCOLLECTION_H

#include <iostream>
#include <string>
#include "playlistitem.h"
#include "song.h"
#include "time.h"

const int MAXSONGS = 1000;


struct SongCollection {
  int numSongs;

  struct Song songs[ MAXSONGS ];
  Time totalTime;
  SongCollection();
  PlaylistItem item(std::string title, std::string artist);

/**
 * Add a single song to a collection.
 */
void addSong (Song song);

/**
 * Add into one collection all songs from another collection that
 * match a given artist and/or title.
 */
void addMatchesFor (
		    SongCollection& toCollection,
		    std::string artist,
		    std::string title);

/**
 * Print a collection to the indicated output stream
 */
void printAll (std::ostream& out);

/**
 * Attempt to read a collection of songs from an input stream,
 * adding it to the collection if possible,
 */
bool readAll (std::istream& in);


};




#endif

playlistItem.cpp
#include "playlistitem.h"
#include "csv.h"

#include <ios>

using namespace std;


/*
 * A PlaylistItem is a "description" of a desired song.
 * It may specify one or more of:  title, artist, album.
 * If any of these are given as an empty string, this is
 * interpreted to mean that any value in this field will do.
 *
 * For example, if we give a title and artist but have "" for
 * the album, then we would match any song with that title by
 * that artist, regardless of which album it is on.
 */





std::string lowercase (const std::string s)
{
  string result = s;
  for (int i = 0; i < result.size(); ++i)
    {
      char c = result[i];
      if (c >= 'A' && c <= 'Z')
	result[i] = c - 'A' + 'a';
    }
  return result;
}


PlaylistItem::PlaylistItem (std::string theTitle, std::string theArtist)
{
  artist = theArtist;
  title = theTitle;
}



bool PlaylistItem::matches (const Song& song)
{
  //cerr << "t@" << title << "\t" << "a@" << artist << " : " << song.title << "\t" << song.artist << endl;
  bool match = true;
  if (title.size() > 0)
    {
      match = (title == lowercase(song.title));
    }
  if (match && artist.size() > 0)
    {
      match = (artist == lowercase(song.artist));
    }
  return match;
}



bool PlaylistItem::read (std::istream& in)
{
  string line;
  if (getline(in, line))
    {
      vector<string> fields = parseCSV(line);
      if (fields.size() == 2)
	{
	  artist = lowercase(fields[0]);
	  title = lowercase(fields[1]);
	}
      else
	{
	  in.setstate (ios::badbit);
	}
    }
  return (bool)in;
}

playlistItem.h
#ifndef PLAYLISTITEM_H
#define PLAYLISTITEM_H

#include <iostream>
#include <string>

#include "song.h"

  /*
   * A PlaylistItem is a "description" of a desired song.
   * It may specify one or more of:  title, artist.
   * If either of these are given as an empty string, this is
   * interpreted to mean that any value in this field will do.
   *
   * For example, if we give an actual title but have "" for
   * the artist, then we would match any song with that title by
   * any artist.
   *
   * All matches should ignore upper/lower case.
   */


struct PlaylistItem {
  std::string title;
  std::string artist;

  PlaylistItem (std::string theTitle, std::string theArtist);

  bool matches (const Song& song);

  bool read (std::istream& in); 
};


#endif
Last edited on
Topic archived. No new replies allowed.