Overloading operators

I need help understanding overloading operators

Page 1 of 1

2 Replies - 876 Views - Last Post: 03 November 2008 - 08:50 AM Rate Topic: -----

#1 cannsyl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-November 08

Overloading operators

Post icon  Posted 03 November 2008 - 08:11 AM

Here is my code, but I keep getting type errors on my ostream...it says ostream is invalid type...
I just don't understand how to overload the operators to make them work. Thanks

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

using namespace std;


class Song
{
   Song ostream& operator<< (ostream& output, int& , string&, string&);
   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();
   private:
	  string title;	//dynamic allocation
	  string artist;
	  int size;
};



// file song.cpp
#include "song.h"

ostream& operator<< ();
//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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Overloading operators

#2 GWatt   User is offline

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: Overloading operators

Posted 03 November 2008 - 08:39 AM

try #include <ostream>
That error means that ostream isn't defined in the scope of you program.
Was This Post Helpful? 0
  • +
  • -

#3 cannsyl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-November 08

Re: Overloading operators

Posted 03 November 2008 - 08:50 AM

Thanks but I am still getting errors.
Here is what I have...

//file song.h
#include <stdlib.h>
#include <iostream>
#include <ostream>
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 & s);
   private:
	  string title;	//dynamic allocation
	  string artist;
	  int size;
};


// file song.cpp
#include "song.h"

Song ostream& operator<< (ostream & os, ostream & os2, const Song & s);
 {
	  os<<s.title;
	  return os;
	  os2<<s.artist;
	  return os2;
 }
//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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1