Discussion:
including a class in my project
(too old to reply)
Larry
2010-01-14 11:35:28 UTC
Permalink
Hi,

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"

int main()
{
myclass* C = new myclass();
(...)
delete C;
return 0;
}

Actually, in the "solution explorer" I did not add those existing items as I
thought it'd fine for them to be just in the the same dir where the main.cpp
file is:

.../Visual Studio 2008/Projects/myproject/myproject/main.cpp, myclass.h,
myclass.cpp and Debug folder along with a file USER.

Now, when I debug the code I showed above I get this error: "unresolved
external symbol" regarding both the constructor and destructor, obiviously.

what am I doing wrong? Also, I tried the same with an. obj file (created
from myclass.cpp file) and I get the same error...

can anyone point me in the right direction, please?

thanks
Ulrich Eckhardt
2010-01-14 12:19:05 UTC
Permalink
Post by Larry
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
#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: ...").
Post by Larry
myclass* C = new myclass();
(...)
delete C;
Note: Don't use dynamic allocation with new/delete unless you have to. C++
is not Java!
Post by Larry
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.
Post by Larry
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
Giovanni Dicanio
2010-01-14 12:26:42 UTC
Permalink
Post by Larry
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
[...]
Post by Larry
Actually, in the "solution explorer" I did not add those existing items as
I thought it'd fine for them to be just in the the same dir where the
.../Visual Studio 2008/Projects/myproject/myproject/main.cpp, myclass.h,
myclass.cpp and Debug folder along with a file USER.
Now, when I debug the code I showed above I get this error: "unresolved
external symbol" regarding both the constructor and destructor, obiviously.
I think that you should add at least the myclass.cpp file in solution
explorer.

Giovanni
Tim Roberts
2010-01-15 05:52:40 UTC
Permalink
Post by Larry
Actually, in the "solution explorer" I did not add those existing items as I
thought it'd fine for them to be just in the the same dir where the main.cpp
No. The only files that will actually be compiled and linked are the ones
that are shown in "solution explorer".
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Continue reading on narkive:
Loading...