Class constructors

Can someone please tell me what is wrong with this?
This is what I have to do and the driver was given.
The first step is to create a C++ class for a song. Each song contains private data consisting of a title, artist
and size in megabytes. The title and artist are strings, and the size has type int. The song class must have
a default constructor, a constructor that initializes each of the data members, get and set functions for
each data member, and an overloaded cout operator. The class declaration is in file Song.h and the function
implementations are in Song.cpp. The class must work properly with the driver posted on the class website.

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
// driver.cpp

//Adapted from Roger Priebe
//CS2308 11/13/07

#include <stdlib.h>
#include <iostream>
#include "song.h"

using namespace std;

int main()
{
    
    Song s1("Frank Sinatra", "My way", 4);
    Song s2("Beatles", "Act Naturally", 5);
    Song s3;
    
    cout << "Song 1" << s1 << endl;
    cout << "Song 2" << s2 << endl;
    cout << "Song 3" << s3 << endl;

    s3.setArtist("Buck Owens");
    s3.setTitle("Act Naturally");
    s3.setSize(3);

    cout << "Song 3 updated " << s3 << endl;


    cout << "Artist 1 (Frank Sinatra)  " << s1.getArtist() << endl;

    cout << "Title 2 (Act Naturally)  " << s2.getTitle() << endl;

    s1.setSize(7);

    cout << "Size 1 (7) " << s1.getSize() << endl;
    
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//file song.h
#include <stdlib.h>
#include <iostream>

using namespace std;


class Song
{
   public:
      Song::Song(string a, string t, int s); // constructor
      void setSize(int s);
      double getSize(); // access or
      void setTitle (string t);
      string getTitle();
      void setArtist (string a);
      string getArtist();
   private:
      string title;    //dynamic allocation
      string artist;
      int size;
};


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
// file song.cpp
#include "song.h"

//constructor
Song::Song(string a, string t, int s); // constructor
{
    size = s;
    title = new string ( strlen(t) + 1 );
    strcpy ( title, t );
    artist = new stromg ( strlen(a) + 1 );
    strcpy ( artist, a )
}
//accessor for name
string Song :: getTitle()
{
      return title;
}
//mutator
void Song :: setTitle (string t)
{
     name = new string (strlen (t) + 1 );
     strcpy (title, t );
}
string Song :: getArtist()
{
      return artist;
}
//mutator
void Song :: setArtist (string a)
{
     name = new string (strlen (a) + 1 );
     strcpy (artist, a );
}
void Song::setSize(int s)
{
    size = s;
}
double Song::getSize()
{
    return size;
}
What is the error it gave you or what was the problem your having with it.
the error was --no matching function for call to "Song::Song" on line 17 of song.cpp file
thanks
It might be because you need to define a default constructor, if you define a custom one, the default one isn't ever made for you.
Ok I changed my code to this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdlib.h>
#include <iostream>

using namespace std;


class Song
{
   public:
      Song::Song ();
      Song::Song(string a, string t, int s); // constructor
      void setSize(int s);
      double getSize(); // access or
      void setTitle (string t);
      string getTitle();
      void setArtist (string a);
      string getArtist();
   private:
      string title;    //dynamic allocation
      string artist;
      int size;
};

but it tells me that declaration of Song::Song outside of class is not definition in my song.cpp file
i'm not sure if the code was copy/pasted into here, but if so, line 11 (strcpy ( artist, a )) is missing a semicolon, which can often greatly confuse compilers, and not always produce a useful error. most compilers will also complain if you do not make sure there is an empty line at the end of your file. good luck.
Thanks so much that did help,
but how do I overload the cout operator
it is telling me that there is no match for operator <<
Just to point something else out, your use of "Song::" within the declaration of the class is not standards compliant I believe.
your welcome, glad i could help. good point jsmith! @cannsyl: when you are within a class declaration, it is pretty much exactly like a normal function prototype return_type function_name (parameters);. scope resolution is unnecessary since you are within the class definition. as to overloading c++ operators is actually almost exactly like writing a member function for a class, this should be the info you need: <http://www.cplusplus.com/doc/tutorial/classes2.html >
Last edited on
Ok here is what I got for overloading operators, but it tells me that something is wrong I need a ; before the &???

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
//song.h
#include <stdlib.h>
#include <iostream>

using namespace std;


class Song
{
  
   public:
      Song::Song ();
      Song::Song(string , string , int ); // constructor
      void setSize(int s);
      double getSize(); // access or
      void setTitle (string t);
      string getTitle();
      void setArtist (string a);
      string getArtist();
      friend ostream& operator<<(ostream &os, const Song &obj);
   private:
      string title;    //dynamic allocation
      string artist;
      int size;
};
 


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
// file song.cpp
#include "song.h"

Song ostream& operator<< (ostream& os, const Song& obj);
 {
      os<<obj.title;
      return os;
      os<<obj.artist;
      return os;
 }
//constructor
Song::Song(string a, string t, int s); // constructor
{
    size = s;
    title = new string ( strlen(t) + 1 );
    strcpy ( title, t );
    artist = new stromg ( strlen(a) + 1 );
    strcpy ( artist, a );
}
//accessor for name
string Song :: getTitle()
{
      return title;
}
//mutator
void Song :: setTitle (string t)
{
     name = new string (strlen (t) + 1 );
     strcpy (title, t );
}
string Song :: getArtist()
{
      return artist;
}
//mutator
void Song :: setArtist (string a)
{
     name = new string (strlen (a) + 1 );
     strcpy (artist, a );
}
void Song::setSize(int s)
{
    size = s;
}
double Song::getSize()
{
    return size;
}
Its never a good idea to just add friends to classes. Theres a different way to overload to operator.

Ex
1
2
3
4
5
6
7
8
9
10
// inside class
void display(ostream& o) {
   // FORMAT OF DISPLAY
}

// outside class
ostream& operator<<(ostream& o, Song& s) {
   s.display(o);
   return o;
}


Its an alternative to using the friend function, does the same thing.

I think your error comes from the .cpp file:
Song ostream& operator<< (ostream& os, const Song& obj);

which needs to be:
ostream& Song::operator<< (ostream& os, const Song& obj);
Last edited on
In general, widespread use of friends is a sign of incorrect encapsulation. However,
when talking about operators, its hard to say whether operators should be members or friends. For example, the boost operators library declares all of the operators to be friend functions.
Topic archived. No new replies allowed.