Thread: Linked List Problems

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I mean:
    Code:
    #include <list>
    In addition to the other header files that you already need.

    EDIT:
    At a glance, one objection I have to Nor's suggested class design is that the std::list<Song>::iterator named Iter should not be a member variable. It should be a local variable of whatever member function needs such an iterator.

    EDIT #2:
    Oh, and isfull(), getTotalMemory(), getRemainingMemory() and the overloaded operator<< are all not const-correct. The member functions should be declared const, and the overloaded operator<< should take the Song by const reference.
    Last edited by laserlight; 11-22-2008 at 02:59 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    where can I declare
    Code:
    int totalMemory = 0;
        int usedMemory = 0;
        int currentSize = 0;
    in the .h or the cpp ? in the constructor or before? private or public? should I make the static const? I didnot think that was the answer but Im not sure anymore? everywhere I think I should put it, it tells me it either forbids initiallization or they are undeclared.

  3. #33
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I gotta go run an errand...I appreciate all the help really...I am trying to learn and understand this stuff. Will any of you be back this afternoon or tomorrow? Thanks so much again

  4. #34
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I emailed my professor earlier. She says we can not use std::list<>...
    Back to the drawing board

  5. #35
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    If i was you. Coffee Coffee Coffee
    Your code is on the right track but i can't wright it for you.
    You just need to do a lot of error checking in your code.
    I understand why your instructor is forcing you to create a linked list.

    Try writing just the list code first. And always make sure it can compile. between you writing portions of code. Even if it doensn't work. Make it Compile. this will find the simple syntax error your making.
    get your list working Then try getting fancy. There are lots of implementations out there.
    Just remember - plagiarism is wrong. Get help but don't cheat.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  6. #36
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I think I am getting it...maybe...
    how do I get information from my class into my linked list?
    I have
    Code:
    int BobCatPod::getTotalMemory()
    {
        totalMemory = s.size;
        return totalMemory;
    }
    where s is an instance of class Song and size is a variable of s. I need to say that the totalMemory is equal to the size in an instance of the class. and return that size.

  7. #37
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    Ok I have everything put together and my files compile, but I am getting an error in the poddriver which was given to us. Here is what I have...the error i am getting is no matching function for call to BobCatPod::BobCatPod(int)
    in line 19 of the poddriver file. thanks
    Code:
    // 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;
    }
    Code:
    //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
    Code:
    // 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;
    }
    Code:
    //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
    Code:
    //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;
    }
    thanks again

  8. #38
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    The reason for this error is you have not made a constructor for BobCatPod that takes an int.
    What is the int argument for?
    Code:
    BobCatPod::BobCatPod(int i);//In your header file
    
    BobCatPod::BobCatPod(int i)  //This goes in your cpp file
    {
        //I'm guessing this is how much memory your pod has.
        if( i <= MAX_MEMORY)
            remainingMemory = i;    
        else
            remainingMemory = MAX_MEMORY;//The assignment only allows 256. No more.
    }
    good luck.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  9. #39
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    ok just one more question about overloading operators.
    If I have included
    Code:
    friend ostream& operator<<(ostream & os, Song & s);
    in my .h files why do i get the error no match for 'operator <<' in '(std:perator<<?
    why does it need another overload?

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It does not need another overload. The problem is that you should be passing the Song by const reference:
    Code:
    friend ostream& operator<<(ostream & os, const Song & s);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I tried that
    Code:
    friend ostream& operator<<(ostream & os, const Song & s);
    in my .h instead of the
    Code:
    friend ostream& operator<<(ostream & os, Song & s);
    but I get the same error.

  12. #42
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    Ok I think I only have one more problem.

    In my .h I have
    Code:
    friend ostream & operator<<(ostream & os, const Song & s);
    and in my cpp
    Code:
    ostream & operator << ( ostream & os, Song & s)
     {
          os << s.getTotalMemory();
          os  << s.getRemainingMemory();
          return os;
     }
    and I need it for
    Code:
     BobCatPod p(256);
        BobCatPod q(512);
        BobCatPod r(25);
    
    
        cout << "pod p" << endl << p << endl;
    but it doesn't work. Why the error says no match for 'operator<<'

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    BobCatPod and Song appear to be two different classes. Also, note that your function declaration (prototype) should match the function definition (const Song& s in both cases).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #44
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    You guys have been great. I now have everything compiling, but I can't get it to run. I keep getting a linker error? I don't know why it doesn't link I have all my files included I think. What would make it do this? what should I check? Thanks

  15. #45
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Nor View Post
    Code:
         newNode = new SongNode;
         newNode->s = num;//<<<<<NEVER use new memory without error checking.
    Why not? new is guaranteed to return a valid object unless you are using nothrow. If the allocation fails an exception is thrown.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 03:54 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM