468,114 Members | 2,115 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,114 developers. It's quick & easy.

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

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:8 3:
undefined reference to `Queue<char>::append(char const&)'
G:\Projects\Datastructure\Queue\Debug\main.o(.text +0x4e5):G:\Projects\Datastructure\Queue\main.cpp:9 2:
undefined reference to `Queue<char>::serve()'
G:\Projects\Datastructure\Queue\Debug\main.o(.text +0x56b):G:\Projects\Datastructure\Queue\main.cpp:1 02:
undefined reference to `Queue<char>::empty() const'
G:\Projects\Datastructure\Queue\Debug\main.o(.text +0x581):G:\Projects\Datastructure\Queue\main.cpp:1 03:
undefined reference to `Queue<char>::retrieve(char&) const'
G:\Projects\Datastructure\Queue\Debug\main.o(.text +0x5b0):G:\Projects\Datastructure\Queue\main.cpp:1 05:
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?
Mar 13 '06 #1
3 4782
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.
Mar 13 '06 #2
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.

Mar 13 '06 #3
"Kceiw" <kc***@163.com> wrote in message news:dv**********@news.cn99.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:8 3:
undefined reference to `Queue<char>::append(char const&)'
G:\Projects\Datastructure\Queue\Debug\main.o(.text +0x4e5):G:\Projects\Datastructure\Queue\main.cpp:9 2:
undefined reference to `Queue<char>::serve()'
G:\Projects\Datastructure\Queue\Debug\main.o(.text +0x56b):G:\Projects\Datastructure\Queue\main.cpp:1 02:
undefined reference to `Queue<char>::empty() const'
G:\Projects\Datastructure\Queue\Debug\main.o(.text +0x581):G:\Projects\Datastructure\Queue\main.cpp:1 03:
undefined reference to `Queue<char>::retrieve(char&) const'
G:\Projects\Datastructure\Queue\Debug\main.o(.text +0x5b0):G:\Projects\Datastructure\Queue\main.cpp:1 05:
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).
Mar 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Evan Simpson | last post: by
5 posts views Thread by Danny Anderson | last post: by
18 posts views Thread by Tuckers | last post: by
2 posts views Thread by Zhaozhi Gao | last post: by
9 posts views Thread by Richard Lionheart | last post: by
5 posts views Thread by caicai | last post: by
3 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.