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

K

Kceiw

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?
 
G

Gernot Frisch

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.
 
R

Rolf Magnus

Kceiw said:
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.
 
J

Jim Langston

Kceiw said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top