what's the difference between #include "queue.h" and #include "queue.cpp"

Discussion in 'C++' started by Kceiw, Mar 13, 2006.

  1. Kceiw

    Kceiw Guest

    Dear all,
    When I use #include "queue.h", I can't link it.
    The error message follows:
    Linking...
    G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In
    function `main':
    G:\Projects\Datastructure\Queue\main.cpp:16: undefined reference to
    `Queue<char>::Queue()'
    G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x394): In
    function `Z10do_commandcR5QueueIcE':
    G:\Projects\Datastructure\Queue\main.cpp:65: undefined reference to
    `Queue<char>::retrieve(char&) const'
    G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x473):G:\Projects\Datastructure\Queue\main.cpp:83:
    undefined reference to `Queue<char>::append(char const&)'
    G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x4e5):G:\Projects\Datastructure\Queue\main.cpp:92:
    undefined reference to `Queue<char>::serve()'
    G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x56b):G:\Projects\Datastructure\Queue\main.cpp:102:
    undefined reference to `Queue<char>::empty() const'
    G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x581):G:\Projects\Datastructure\Queue\main.cpp:103:
    undefined reference to `Queue<char>::retrieve(char&) const'
    G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x5b0):G:\Projects\Datastructure\Queue\main.cpp:105:
    undefined reference to `Queue<char>::serve()'
    collect2: ld returned 1 exit status

    But when I use #include "queue.cpp", everything goes right.
    Should we use #include "queue.h" when we use a class? Why it doesn't work?
     
    Kceiw, Mar 13, 2006
    #1
    1. Advertising


  2. > But when I use #include "queue.cpp", everything goes right.
    > Should we use #include "queue.h" when we use a class? Why it doesn't
    > work?



    Yuck!

    #include "queue.h" // now the compiler knows the definitions and
    declarations required for Quele class.

    Include the Queue.cpp into your project.
     
    Gernot Frisch, Mar 13, 2006
    #2
    1. Advertising

  3. Kceiw

    Rolf Magnus Guest

    Kceiw wrote:

    > Dear all,
    > When I use #include "queue.h", I can't link it.
    > The error message follows:
    > Linking...
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In
    > function `main':
    > G:\Projects\Datastructure\Queue\main.cpp:16: undefined reference to
    > `Queue<char>::Queue()'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x394): In
    > function `Z10do_commandcR5QueueIcE':
    > G:\Projects\Datastructure\Queue\main.cpp:65: undefined reference to
    > `Queue<char>::retrieve(char&) const'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x473):G

    \Projects\Datastructure\Queue\main.cpp:83:
    > undefined reference to `Queue<char>::append(char const&)'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x4e5):G

    \Projects\Datastructure\Queue\main.cpp:92:
    > undefined reference to `Queue<char>::serve()'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x56b):G

    \Projects\Datastructure\Queue\main.cpp:102:
    > undefined reference to `Queue<char>::empty() const'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x581):G

    \Projects\Datastructure\Queue\main.cpp:103:
    > undefined reference to `Queue<char>::retrieve(char&) const'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x5b0):G

    \Projects\Datastructure\Queue\main.cpp:105:
    > undefined reference to `Queue<char>::serve()'
    > collect2: ld returned 1 exit status


    Yes. You forgot to link your Queue implementation.

    > But when I use #include "queue.cpp", everything goes right.
    > Should we use #include "queue.h" when we use a class?


    Yes.

    > Why it doesn't work?


    Because you didn't tell your build system that queue.cpp contains something
    that is needed. It must be compiled and linked to your program. How that is
    done depends on your build system/IDE or whatever you use.
     
    Rolf Magnus, Mar 13, 2006
    #3
  4. Kceiw

    Jim Langston Guest

    "Kceiw" <> wrote in message news:dv3qiu$kgs$99.com...
    > Dear all,
    > When I use #include "queue.h", I can't link it.
    > The error message follows:
    > Linking...
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In
    > function `main':
    > G:\Projects\Datastructure\Queue\main.cpp:16: undefined reference to
    > `Queue<char>::Queue()'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x394): In
    > function `Z10do_commandcR5QueueIcE':
    > G:\Projects\Datastructure\Queue\main.cpp:65: undefined reference to
    > `Queue<char>::retrieve(char&) const'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x473):G:\Projects\Datastructure\Queue\main.cpp:83:
    > undefined reference to `Queue<char>::append(char const&)'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x4e5):G:\Projects\Datastructure\Queue\main.cpp:92:
    > undefined reference to `Queue<char>::serve()'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x56b):G:\Projects\Datastructure\Queue\main.cpp:102:
    > undefined reference to `Queue<char>::empty() const'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x581):G:\Projects\Datastructure\Queue\main.cpp:103:
    > undefined reference to `Queue<char>::retrieve(char&) const'
    > G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x5b0):G:\Projects\Datastructure\Queue\main.cpp:105:
    > undefined reference to `Queue<char>::serve()'
    > collect2: ld returned 1 exit status
    >
    > But when I use #include "queue.cpp", everything goes right.
    > Should we use #include "queue.h" when we use a class? Why it doesn't work?


    You need to #include "queue.h" and also add the queue.cpp to your project.
    The step you are missing is adding queue.cpp to your project. It also needs
    to be compiled into an object file.
    Hmm. I see .o files and not .obj so it looks like you're using gpp. Not
    sure how to add to gpp type projects (if there are even project files).
     
    Jim Langston, Mar 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2hpanUgUG95aWxpbA==?=

    Difference Between UserControl and Include Page

    =?Utf-8?B?U2hpanUgUG95aWxpbA==?=, Jul 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    340
    John Timney \(Microsoft MVP\)
    Jul 28, 2004
  2. jakk
    Replies:
    4
    Views:
    12,607
  3. Santa
    Replies:
    1
    Views:
    1,153
    Mark A. Odell
    Jul 17, 2003
  4. PerlFAQ Server
    Replies:
    0
    Views:
    393
    PerlFAQ Server
    Jan 6, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    315
    PerlFAQ Server
    Apr 15, 2011
Loading...

Share This Page