linked list with class in multiple files
Nov 23, 2008 at 3:27am UTC
I need to get the size of a song into a linked list of classes.
here is my code...I am getting an error that says getsize is undeclared??? and I don't know why because I included the .h file.
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
//song.h
#ifndef SONG_H
#define SONG_H
#include <stdlib.h>
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
class Song
{
public :
Song();
Song(string , string , int ); // constructor
void setSize(int s);
int getSize(); // access or
void setTitle (string t);
string getTitle();
void setArtist (string a);
string getArtist();
void addSong (int );
friend ostream& operator <<(ostream & os, Song & s);
private :
string title; //dynamic allocation
string artist;
int size;
};
#endif
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
// file song.cpp
#include "song.h"
#include "BobCatPod.h"
ostream & operator << ( ostream & os, Song & s)
{
os << s.getTitle();
os << s.getArtist();
os << s.getSize();
return os;
}
//constructor
Song::Song ()
{
title=" " ;
artist=" " ;
size = 0;
}
Song::Song(string a, string t, int s) // constructor
{
size = s;
title = t;
artist = a ;
}
//accessor for name
string Song :: getTitle()
{
return title;
}
//mutator
void Song :: setTitle (string t)
{
title = t;
}
string Song :: getArtist()
{
return artist;
}
//mutator
void Song :: setArtist (string a)
{
artist = a;
}
void Song::setSize(int s)
{
size = s;
}
double Song::getSize()
{
return 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
//BobCatPod.h
//Specification files for the SongList class
#ifndef BOBCATPOD_H
#define BOBCATPOD_H
#include "song.h"
class BobCatPod
{
private :
struct SongNode
{
Song s; //value in this node
SongNode *next;
};
SongNode *head;
static const int MAX_MEMORY = 256;
public :
int totalMemory;
//Constructor
BobCatPod();
//Linked list operations
bool isFull ( ) const ;
int addSong (Song s);
int removeSong (Song s);
void showSongList ();
int getTotalMemory();
int getRemainingMemory();
friend ostream& operator <<(ostream & os, Song & s);
//Destructor
~BobCatPod();
};
#endif
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
//BobCatPod.cpp
#include "song.h"
#include "BobCatPod.h"
BobCatPod::BobCatPod()
{
head = NULL;
}
bool BobCatPod::isFull ( ) const
{
if (totalMemory > MAX_MEMORY)
{
return true ;
}
}
int BobCatPod::addSong(Song t)
{
if (isFull ())
{
return 0;
}
SongNode *newSong = new SongNode; //a new node
// Allocate a new node and stor num there.
newSong->s = t;
head = newSong;
newSong->next = NULL;
totalMemory++;
return 1;
}
int BobCatPod::removeSong( Song t )
{
if (isFull ())
{
totalMemory--;
return 1;
}
else
return 1;
}
void BobCatPod::showSongList( )
{
SongNode *n;
cout<<endl;
for ( n = head ; n != NULL ; n = n->next )
cout<<endl<<n->s;
}
int BobCatPod::getTotalMemory()
{
totalMemory = t.getSize;
return totalMemory;
}
thanks
Nov 23, 2008 at 3:37am UTC
It's because getsize() returns an int in your .h, but in your .cpp it returns a double.
Nov 23, 2008 at 3:49am UTC
I see that now, but it still gives me an error that says for
1 2 3 4 5 6
int BobCatPod::getTotalMemory()
{
Song t;
totalMemory = t.getSize;
return totalMemory;
}
argument of type 'int (Song::)()' does not match 'int'
Nov 23, 2008 at 3:50am UTC
getSize is a function, you need the () on it.
Nov 23, 2008 at 3:52am UTC
Thanks so much...I totally appreciate it.
Nov 23, 2008 at 3:53am UTC
No problem :)
Nov 23, 2008 at 4:11am UTC
OK I have put it all together but I am getting an error that says no matching function for call to 'BobCatPod::BobCatPod(int)'
in my poddriver line 19???I included the .h so I don't get it
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
// poddriver.cpp
//Adapted from Roger Priebe
//CS2308 11/17/08
#include <stdlib.h>
#include <iostream>
#include "BobCatPod.h"
using namespace std;
int main()
{
Song s1("Frank Sinatra" , "My way" , 14);
Song s2("Beatles" , "Act Naturally" , 5);
Song s3;
BobCatPod p(256);
BobCatPod q(512);
BobCatPod r(25);
cout << "pod p" << endl << p << endl;
cout << "pod q, size 512 exceeds max, should be reset to 256 " << endl
<< q << endl;
cout << "pod r, size should be 25 " << endl << r << endl;
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(20);
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;
//test relational operators
if (s1 < s3 ) cout << endl << "s1 < s3" << endl;
else cout << endl << "s1 >= s3" << endl;
if (s1 == s2) cout << endl << "s1 == s2" << endl;
else cout << endl << "s1 != s2" << endl;
// test addnode
cout << "add song 1, size 14 " << r.addSong(s1) << endl;
cout << " memory left = " << r.getRemainingMemory() <<endl;
cout << "add song 2, size 5 " << r.addSong(s2)<< endl;
cout << " memory left = " << r.getRemainingMemory() << endl;
cout << "add song 3, size 20 should fail " << r.addSong(s3) <<endl;
cout << " memory left = " << r.getRemainingMemory() << endl;
s3.setSize(2);
cout << "Size 3 (2) " << s3.getSize() << endl;
cout << "add song 3, size 2 should succeed " << r.addSong(s3)
<< " memory left = " << r.getRemainingMemory() << endl;
// test output
cout << r << endl;
//test delete
r.removeSong(s1);
cout << r << endl;
r.removeSong(s3);
cout << r << endl;
cout << "Total Memory = " << r.getTotalMemory() << 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 23 24 25 26 27 28 29 30 31
//song.h
#ifndef SONG_H
#define SONG_H
#include <stdlib.h>
#include <iostream>
#include <ostream>
#include <string>
#include "BobCatPod.h"
using namespace std;
class Song
{
public :
Song();
Song(string , string , int ); // constructor
void setSize(int s);
int getSize(); // access or
void setTitle (string t);
string getTitle();
void setArtist (string a);
string getArtist();
void addSong (int );
friend ostream& operator <<(ostream & os, Song & s);
private :
string title; //dynamic allocation
string artist;
int size;
};
#endif
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
// file song.cpp
#include "song.h"
#include "BobCatPod.h"
ostream & operator << ( ostream & os, Song & s)
{
os << s.getTitle();
os << s.getArtist();
os << s.getSize();
return os;
}
//constructor
Song::Song ()
{
title=" " ;
artist=" " ;
size = 0;
}
Song::Song(string a, string t, int s) // constructor
{
size = s;
title = t;
artist = a ;
}
//accessor for name
string Song :: getTitle()
{
return title;
}
//mutator
void Song :: setTitle (string t)
{
title = t;
}
string Song :: getArtist()
{
return artist;
}
//mutator
void Song :: setArtist (string a)
{
artist = a;
}
void Song::setSize(int s)
{
size = s;
}
int Song::getSize()
{
return 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 49 50
//BobCatPod.h
//Specification files for the SongList class
#ifndef BOBCATPOD_H
#define BOBCATPOD_H
#include "song.h"
class BobCatPod
{
private :
struct SongNode
{
Song s; //value in this node
SongNode *next;
};
SongNode *head;
static const int MAX_MEMORY = 256;
public :
int totalMemory;
int remainingMemory;
//Constructor
BobCatPod();
//Linked list operations
bool isFull ( ) const ;
int addSong (Song s);
int removeSong (Song s);
void showSongList ();
int getTotalMemory();
int getRemainingMemory();
friend ostream& operator <<(ostream & os, Song & s);
//Destructor
~BobCatPod();
};
#endif
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
//BobCatPod.cpp
#include "song.h"
#include "BobCatPod.h"
BobCatPod::BobCatPod()
{
head = NULL;
}
bool BobCatPod::isFull ( ) const
{
if (totalMemory > MAX_MEMORY)
{
return true ;
}
}
int BobCatPod::addSong(Song t)
{
if (isFull ())
{
return 0;
}
SongNode *newSong = new SongNode; //a new node
// Allocate a new node and stor num there.
newSong->s = t;
head = newSong;
newSong->next = NULL;
totalMemory++;
return 1;
}
int BobCatPod::removeSong( Song t )
{
if (isFull ())
{
totalMemory--;
return 1;
}
else
return 1;
}
void BobCatPod::showSongList( )
{
SongNode *n;
cout<<endl;
for ( n = head ; n != NULL ; n = n->next )
cout<<endl<<n->s;
}
int BobCatPod::getTotalMemory()
{
Song t;
totalMemory = t.getSize();
return totalMemory;
}
int BobCatPod::getRemainingMemory()
{
remainingMemory = MAX_MEMORY - totalMemory;
return remainingMemory;
}
Nov 23, 2008 at 4:36am UTC
You mean this line?
BobCatPod p(256);
1 2
//Constructor
BobCatPod();
Mmh... Nope. No matching function there.
What exactly did you mean to do?
Dec 3, 2008 at 5:38am UTC
I think you are in my class. Did you finish prog 5? I still have not figured it out. If you could assist me that would be cool.
Topic archived. No new replies allowed.