Thread: Linked List Problems

  1. #46
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Force of habit. Plus, there is no telling what system this code will run on.
    If its running on a new high powered pc then your right.
    If its running on a Wayne fuel dispenser, your wrong.
    Can not till you about an I-pod because i've never programed one. But i'm willing to bet a simple void pointer check will not crash the system as fast as processing a void pointer.
    c++ is not just for personal computers anymore.

    what linker error are you getting?
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  2. #47
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nor
    Force of habit. Plus, there is no telling what system this code will run on.
    If its running on a new high powered pc then your right.
    If its running on a Wayne fuel dispenser, your wrong.
    We are talking about a rule mandated by the C++ Standard.

    Quote Originally Posted by Nor
    Can not till you about an I-pod because i've never programed one. But i'm willing to bet a simple void pointer check will not crash the system as fast as processing a void pointer.
    I think you mean null pointer, not void pointer, but of course new is not guaranteed to return a null pointer if memory could not be allocated, unless the nothrow version of new is used.
    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

  3. #48
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    the linker error i am getting is
    undefined reference to 'operator<<(std:stream&, Song const&)'

  4. #49
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cannsyl View Post
    the linker error i am getting is
    undefined reference to 'operator<<(std:stream&, Song const&)'
    So what does your function definition look like?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #50
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    my function definition is
    Code:
    ostream & operator << ( ostream & os, BobCatPod & i)
     {
          os << i.getTotalMemory();
          os  << i.getRemainingMemory();
          return os;
     }
    in bobcatpod.h
    and
    Code:
     ostream & operator << ( ostream & os, Song & s)
     {
          os << s.getTitle();
          os  << s.getArtist();
          os << s.getSize();
          return os;
     }
    song.h

  6. #51
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I didn't look at the rest of this thread but:

    have you tried
    Code:
    std::ostream
    and (i just made the following mistake too):

    have you included <iostream> in the right place(s)?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #52
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    I tried adding the std:stream, but that didn't seem to be it.
    I don't know where you mean for me to put the <iostream> I do have it in my .h's

  8. #53
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Nor View Post
    Force of habit. Plus, there is no telling what system this code will run on.
    While that's a valid point, it's better to assume standards compliance until proven otherwise, or unless you know up front that a targetted system isn't compliant. Even if you had to insert a new failure check at some future point, that shouldn't be so bad -- well designed software shouldn't be calling new from that many distinct places, anyway.

    If exceptions are available on your given platform set you should be using them, I guess is my point.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #54
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    ok I am getting an error that says
    in function 'std:stream& operator<<(std:stream&, const Song&):
    invalid use of non-static data member 'Song::title'
    it is in line 15 of this code
    Code:
    //song.h
    #ifndef SONG_H
    #define SONG_H
    #include <stdlib.h>
    #include <iostream>
    #include <ostream>
    #include <string>
    //#include "BobCatPod.h"
    using namespace std;
    //std::ostream;
    
    class Song
    {
       private:
          string title;    //dynamic allocation
          string artist;
          int size;
       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, const Song &s)
          {
          os << title;
          os  << artist;
          os << size;
          return os;
          }
          bool operator==(const Song& lhs)
          {
               if (strcmp (artist, lhs.artist) == 0)
               {if (strcmp (title, lhs.title) ==0)
                {if (size == lhs.size)
                  return true;
                }
                }
                return false;
          }
          bool operator<(const Song& lhs)
          {
               if (artist < lhs.artist)
               { return true;
               }
               else if (artist > lhs.artist)
               {
                   return false;
               }
               else if (title < lhs.title)
               {
                    return true;
               }
               else if (title > lhs.title)
               {
                    return false;
               }
               else if (size < lhs.size)
               {
                    return true;
               }
               else
               {
                   return false;
               }
          }
      
    };
    #endif

  10. #55
    Registered User
    Join Date
    Nov 2008
    Posts
    48
    ok I am getting an error that says
    Code:
    linker error undefined reference to  'Song::artist'
    but I am not sure where in the code it is...
    Code:
    //song.h
    #ifndef SONG_H
    #define SONG_H
    #include <stdlib.h>
    #include <iostream>
    #include <ostream>
    #include <string>
    //#include "BobCatPod.h"
    using namespace std;
    //std::ostream;
    
    class Song
    {
       private:
          string title;    //dynamic allocation
          string artist;
          int size;
       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, const Song &s)
          {
          os << title;
          os  << artist;
          os << size;
          return os;
          }
          bool operator==(const Song& lhs)
          {
               if (strcmp (artist, lhs.artist) == 0)
               {if (strcmp (title, lhs.title) ==0)
                {if (size == lhs.size)
                  return true;
                }
                }
                return false;
          }
          bool operator<(const Song& lhs)
          {
               if (artist < lhs.artist)
               { return true;
               }
               else if (artist > lhs.artist)
               {
                   return false;
               }
               else if (title < lhs.title)
               {
                    return true;
               }
               else if (title > lhs.title)
               {
                    return false;
               }
               else if (size < lhs.size)
               {
                    return true;
               }
               else
               {
                   return false;
               }
          }
      
    };
    #endif
    Last edited by cannsyl; 12-01-2008 at 03:27 PM. Reason: question changed

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