By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,590 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,590 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
"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.