Re: including a class in my project
Larry wrote:
I have coded a class basically made up of 2 file: myclass.h,
myclass.cpp I would like to use this class in one of my project.
So I created a new empty project with a main.cpp file that
looks like this:
#include <stdio.h>
#include "myclass.h"
This only tells the compile "replace this line with the content of this
file". The file only contains an interface definition ("class myclass looks
like this: ...").
myclass* C = new myclass();
(...)
delete C;
Note: Don't use dynamic allocation with new/delete unless you have to. C++
is not Java!
Now, when I debug the code I showed above I get this error: "unresolved
external symbol" regarding both the constructor and destructor,
obiviously.
Note: If you press the debug button, the first thing is to build the
project, and that fails, i.e. you don't get to the debug stage even.
Instead, compiling the project's files works correctly, but when linking
them all into one executable it fails because the implementation of class
myclass is missing.
Also, I tried the same with an. obj file (created
from myclass.cpp file) and I get the same error...
I have no idea how you can even remotely do the same as you did with ...
uhm, with what exactly? :|
The problem is that the project knows (via the #include) what the class
looks like on the surface. The constructor and destructor are underneath
though, but in order to assemble the whole they are needed.
Two options:
1a. Add the .hpp and .cpp files to the project.
1b. #include the .cpp file in you main.cpp.
2. Create a library that contains the .hpp and .cpp files used for class
myclass and link your project against that library.
I'd suggest option 1a for now. 1b is more flexible but a bit hackish still.
Option 2 is what you should learn doing in the long term.
Good luck!
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932