Thread: Classic "undefined reference" problem, headers... :s

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Unhappy Classic "undefined reference" problem, headers... :s

    Hey guys i've been getting this error lately... I think i should learns how to solve it once for all.

    i declare a class "Libro" which stands for Book in english, anyway when i try to define the functions one get This error:
    undefined reference to `Libro::setAll(std::string, std::string, std::string, std::string, std::string)' but apparently to me i did it ok :P(crying in the inside)

    libro.h
    Code:
    #include<string>
    
    
    using namespace std;
    class Libro
    {    
            string titulo;
            string autor;
            string editora;
            string fechaPublicacion;
            string categoria;
            Libro *siguiente;
            //Libro *anterior;
    
    
        public:        
            void setAll(string,string,string,string,string);
            void setSiguiente(Libro *siguiente);
            Libro *getSiguiente();    
            string getCategoria();
            string getAutor();
            string getTitulo();        
    };
    libro.cpp
    Code:
    #include"libro.h"
    
    
    void Libro::setAll(string titulo,string autor,string editora,string fechapu,string categoria)
    {
        this->titulo = nombre;
        this->autor = autor;
        this->editora = editora;
        this->fechaPublicacion = fechapu;
        this->categoria = categoria;
    }
    
    
    void Libro::setSiguiente(Libro *libro)
    {
        this->siguiente = libro;
    }
    Libro* Libro::getSiguiente()
    {
        return this->siguiente;
    }
    string Libro::getCategoria()
    {
        return this->categoria;
    }
    string Libro::getAutor()
    {
        return this->autor;
    }
    string Libro::getTitulo()
    {
        return this->titulo;
    }
    Help please, any recomendations let me know im a notebook with blank pages

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You probably forgot to provide the cpp file during compilation.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    That did it! But surprinsigly i though i got this obviously i don't. Thanks!! By the way i can't i include the .h file instead,
    like those other libraries that all of us include?

    create header>create .cpp to define the header> include the .cpp?

    Thanks

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You do need to include the header, but that's not enough. You also need to sperately build each .cpp file into a .o file, before building the whole program. A good IDE, or something that can automatically generate make files will do this for you.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Ummmm.... that's right! the .o objects gets old(do not update) while my program grows. How Can i solve this? Im using Code::Blocks and even when i press rebuild or even clean, it is still referencing to and .o file not even relevant at the moment... :S

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't Compile a code!!! I ran into "undefined reference to" error.
    By andereasmehdi in forum Linux Programming
    Replies: 19
    Last Post: 06-28-2011, 05:45 AM
  2. Problem with reading/writing in "classic" unix
    By StriderV in forum C Programming
    Replies: 13
    Last Post: 01-02-2011, 08:21 AM
  3. Replies: 4
    Last Post: 12-16-2008, 08:00 PM
  4. Replies: 5
    Last Post: 06-01-2006, 04:37 PM
  5. Replies: 11
    Last Post: 12-14-2005, 02:09 AM