Thread: error with functions

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    error with functions

    hey, if you could just check out my code, i've spent soo long on it already, and i cant seem to figure out WHY this doesnt work when the code for the functions is pretty simliar!
    all im doing is getting input from the stream and storing into a variable.

    Code:
    //animals.h
    class animals {
    public:
     void set_title(string);
     void set_G(string);
    private:
      string G;
      string title;
    };
    
    //animals.cpp
    
    //...
    
    void Item::set_title(string x) {
            title = x;
    }
    void Item::set_G(string x) {
            G = x;
    }

    Code:
    //main.cpp
    
    // ...
    
      Animals * temp;
      string title;
      string G;
    
      out << "Enter title: ";
      getline(in, title);
      temp->set_title(title);
    
      out << "Enter genre: ";
      getline(in,G);
      temp->set_G(G);  //segfaults right here

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Animals * temp;
    No memory has been allocated

    Animals * temp = new Animals;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    OMG you have got to be joking, that WAS the error!!!
    geeeeeezus! how long did i spend on this shnizzle just for that crappy eerror!
    thanks mate

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    don't worry, i've spent more time on easier problems....sometimes you get it stuck in your head that that the problem has to do with one thing (ussually it's your first attempt at it), but it's actually something completely different and fundamental....

    ussually when i get stuck, i'll re-type the code without looking at the original code and can find the stupid mistakes i made that way...ussually i end up typing just a few lines and go "oh! i'm a dipsh-.....i forgot to do 'this'"....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM