Thread: Headache error message

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    46

    Headache error message

    okay, I keep getting this compiler error message:

    Undefined. . . . . . . . . . . . . . . . . first referenced
    symbol. . . .. . . . . . . . . . . . . . . . . . . in file
    rob::Book::~Book() . . . . . . . . . . . /var/tmp//ccZWEnnc.o
    rob::Book::Book() . . . . . . . . . . . . /var/tmp//ccZWEnnc.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect 2: ld returned 1 exit status

    rob::Book::~Book() <===this is the destructor for class Book in namespace rob that I created, the other one is the default constructor. I've tried everything I can think of and more. I need some serious help, I still have a lot left to work on for this project lol

    Code:
    //class header file
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    namespace rob
    {
      class Book
      {
        public:
          ~Book();
          Book (const Book& b1);
          void operator =(const Book& b1);
          Book();
          void print (ostream& outs);
          void fill (istream& ins);
        private:
          string* title;
          string* author;
          int copies, dollars, cents;
      };
    }
    
    //implementation file. I'll only put the destructor and default
    //constructor because those are the problem ones
    
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    #include <cctype>
    #include "book.h" //header file
    
    using namespace std;
    using namespace rob;
    
    namespace rob
    {
      Book::~Book()
      {
        delete title;
        delete author;
       }
       Book::Book()
       {
         title = new string;
         author = new string;
         copies = 0;
         dollars = 0;
         cents = 0;
        }
        //etc etc etc, the rest works fine
    }
    
    //main program, just testing to see if it compiles (which it doesn't)
    
    #include "book.h"
    
    using namespace std;
    using namespace rob;
    
    int main()
    {
      Book b1;
      return 0;
    }
    if you see anything that could be wrong please help. I had it working fine as one file, but when I split it up it stopped working

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I haven't compiled it, but I have a clue.
    Code:
    using namespace std;
    
    namespace rob
    By opening up the std namespace, I bet your rob namespace is inside it. Try the following driver program:
    Code:
    #include "book.h"
    
    using namespace std;
    using namespace rob;
    
    int main()
    {
      std::rob::Book b1;
      return 0;
    }
    edit: Actually, I have even less of a clue after I compiled it. I didn't get any linking errors.
    Last edited by pianorain; 01-31-2005 at 03:21 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    I believe your problem is that you declared you were using a namespace before you defined it.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    I don't think the problem is with the namespace. I defined namespace rob in the header file. I'm just adding the implementation to it in the other one. I don't get an error message with the other functions in my class, just with the default constructor and the destructor

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Strait
    okay, I keep getting this compiler error message:

    Undefined. . . . . . . . . . . . . . . . . first referenced
    symbol. . . .. . . . . . . . . . . . . . . . . . . in file
    rob::Book::~Book() . . . . . . . . . . . /var/tmp//ccZWEnnc.o
    rob::Book::Book() . . . . . . . . . . . . /var/tmp//ccZWEnnc.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect 2: ld returned 1 exit status

    rob::Book::~Book() <===this is the destructor for class Book in namespace rob that I created, the other one is the default constructor. I've tried everything I can think of and more. I need some serious help, I still have a lot left to work on for this project lol


    if you see anything that could be wrong please help. I had it working fine as one file, but when I split it up it stopped working
    How did you comple the program?

    I put your header stuff in "book.h", the implementation stuff in "rob.cpp" and the main stuff in "main.cpp".

    Then:

    g++ rob.cpp main.cpp -Wall -pedantic
    No compiler messages.

    g++ main.cpp
    ...Temp/ccMICfav.o(.text+0x25):main.cpp: undefined reference to `rob::Book::Book[in-charge]()'
    ...Temp/ccMICfav.o(.text+0x30):main.cpp: undefined reference to `rob::Book::~Book [in-charge]()'
    collect2: ld returned 1 exit status
    Regards,

    Dave

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    You have not compiled the implementation file. When you are compiling the main application file the linker is referencing object code that does not yet exist.

    Compile (do not try to link) book.cpp first, then pass book.o to the linker when your compiling main.cpp.
    Last edited by DarkStar; 01-31-2005 at 03:49 PM.
    rwalt

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    HA! success. I just got rid of the pointers and the big 3. I misread the directions my professor gave me. I'm not supposed to use dynamic memory until the second part of this program lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Example of struct in book causing a headache
    By cjohnman in forum C Programming
    Replies: 22
    Last Post: 05-02-2008, 12:23 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Recursion = headache;
    By RoD in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2002, 09:34 PM
  4. My headache
    By Morgan in forum C Programming
    Replies: 7
    Last Post: 12-03-2002, 03:33 AM
  5. Linked List headache
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 09-23-2001, 06:42 PM