I have been a Delphi user for 8 years and now migrated to C++ and have found the changeover quite taxing indeed.

I am using Visual Studio .NET and have before that used MSVC 3 and MSVC 6 and always had the same problem:

I code my classes and methods etc and they all work perfectly.
I then split them into .h and .cpp files and do the #ifndef thingy on the .h file.
I then include both files into the project using the right click:add:existing item method and in my main file I do #include <myclass.h>

EVEY TIME I call a method from my class I get an UNRESOLVED EXTERNAL SYMBOL error message for every method used in my source file.

Removing the .h (iether/or/and) .cpp file gives the same problem. moving the files to a new dir gives me a 'file not found' error till i return i to my include path so I know it finds the file.

The only way i can get my code to compile without error messages is to includ my header file in my class.cpp file and then to do the following in my source file:
#include <myclass.h>
#include <myclass.cpp>

I've tried turning off preprocessor binding etc but this is the only way i have been able to get my code to work...

Why is this and how do i get this to work properly in VisualStudio.net

I am so frustrated that I want to go back to using Delphi, bu for the purpose of distributing my final program on the Mac as well as on Windows, Delphi is not really an option.

Seeing how I am no a fan of using other people's code and rewriting everything to be my own (the current file in question is a linked list class to remove dependance on the VECTOR and LIST classes), I am considering moving over to Borland C since I have a lot of faith on Borland and believe thier IDE won't give me this problem.

Here is my initial concern though:
Is my problem somehow rlated to C++ or entirely the IDE?

Please help. I've been stuck for months now!

Thanks in advance

Recommended Answers

All 7 Replies

First, why are you using the #include <header.h> variation? That is generally used for system headers. User headers are generally #include "header.h". It makes a difference in where it looks for the files.

Second, are you setting up the project correctly for multiple source files? It ought to look something like [post=106169]this[/post].

And you know you shouldn't #include source files, right?

yeah. i got my source files there and my .h files in the header folder below it.
i have tried taking the .h files out of the header section and that works cause i still include it with the #include call

i set up my search path to include '.\header' and '.\src' so that I can use <myclass.h> instead of adding every file with the notation "header\myclass.h"

are you telling me THAT might be why my code doesn't work???? wow! I would never have thought of that! Will try that out asap when i get home.

as for the source files, yeah, what i did was this:
- hit build
- pull my hair out
- copy and paste the content of both .h and .cpp files back into the main .cpp file
- remove the .cpp and .h file from the project
- comment out the #include <myclass.h>
- hit build
- smile
- comment out the entire .h contents from the main file
- uncomment the #include <myclass.h> file
- hit build
- keep smiling
- insert the .h file into the project
- hit build
- still smiling - due to the #ifndef, no doubt
- include the .cpp file into the project as per your pic on your other post
- comment out the contents of myclass.cpp in my main project file
- hit build
- pull my hair out
- add #include <myclass.cpp>
- hit build
- get duplicate class definition error
- comment out #include <myclass.cpp>
- hit build
- UNRESOLVED EXTERNAL SYMBOLS (on my constructors by the way)
- remove myclass.cpp from the project
- uncomment #include <myclass.cpp>
- hit build
- smile again ....

Including the source seems to work, but I know i am not supposed to do so for the simple reason of reusing the same code in multiple files (possily at once) and that is why simply getting my code to work is not good enough, i need to find out why my code doesn't work the way it is supposed to and get it sorted!

Does the prepocessor setting have anything to do with it at all?

Maybe you'll want to start a little smaller. Make a new project and add some empty modules. Then start filling them up.

Also, if your code isn't terribly long, you might want to post the contents here. It's a whole lot easier to bebug things you can see.

With regard to the duplicate definition vs unresolved externals, do your headers only contain declarations and your modules have the definitions?

[edit]Something like this, perhaps.
[IMG]http://img77.echo.cx/img77/2677/project6dq.th.gif[/IMG]

i did a simple test last night.

- I created a brand new project to ensure i didn't mess up any of the settings
- i then right clicked and selected add new class, specified a name and accepted
- i then included the new class's .h file using #include "simpeclass.h"
- i then created a variable of the new class and hit build
- worked like a dream
- i then changed the new class to look like this:

//.H file

template <class T>//new line
class simpleclass
{
public:
     simpleclass();
     ~simpleclass();
};

<< moderator edit: added [code][/code] tags >>

//cpp file

template <class T> //new line
simpleclass<T>::simpleclass() //modified line
{
}

template <class T> //new line
simpleclass<T>::~simpleclass() //modified line
{
}

That is it.
I then went back into my main program and changed my var
from simpleclass newvar;
to simpleclass<int> newvar;

and got the UNRESOLVED EXTERNAL SYMBOL errors again.:rolleyes:
commenting out the variable builds th code without any problems at all so i assume the class is still okey. But then why doesn't it work?

In asnwer to your question, yes, i only define the class and methods in the .h file and do all coding of the methods in the .cpp file

What i did do on one occasion, was to comment out the entire constructor in the cpp file and in the .h file i just modified the code to this:

template <class T>
class simpleclass
{
public:
    simpleclass(){printf("This is working fine\n");}
    ~simpleclass(){}
};

I forgot to mention I am writing a console app as I just want to test my code. i am used to Delphi's way of creating the forms and just using it, i have no idea yet how to load the forms as resources. also this way if i get a runtime error i can still see the printed console statements from before the runtime error...

anyway, so i then hit build and i no longer get the UNRESOLVED EXTERNAL SYMBOLS error message, but now when i run the program using:

printf("Now creating the var\n");
simpleclass<int> *newvar = new simpleclass<int>;
printf("Finished creating variable\n");

I get the following output:
Now creating the var
Finished creating the var

:eek:

On a new note, I usually insert my class def inside

#ifndef filename_h
#define filename_h
#endif

but i noticed when i clicked on create new class, the .h file only had one line at the top
#pragma once

is this an alternate way of doing what i do or is it the new standard?
is it recommended for creating multi platform code?

ABOUT YOUR SUBMITTED PIC
A few things I thought I'd mention.
I took a few screen caps last night but my windows system doesn't have net access so i will connect it to my router when i get home tonight (at class today)

I noticed you use <iostream>
i am trying to make my code completely generic and not dependent on any namespace or libs that might not be available on another platform. for this reason i just use the stdafx header that gets included when i create the console app.

Also I noticed your header.h file is under external dependancies and not under header. could this be my problem?

<off topic>about me<off topic>
just so you know what i'm about. I have been doing delphi for about 1 year after doing pascal for 6. I got bored. Then someone mentioned to me that writing games was a real challenging task so i looked at it and found a nice challenge - eventhough the actual coding was still as simple as using the basic commands, the challange of creating something that was playable was a real nice challenge. 3 months later i had a demo up and running of my version of a final fantasy game. all i had left to do was implement the fighting engine, change the dialogue i created for the characters to form a storyline, add more tasks to perform and then change my blocky world into a fabulous looking reality.

This prompted me to believe that I will, in all certainty, be able to create a fully running game, but then I moved to a new country and around 1 year later i bought a new system and found the graphics lib I was using was no longer available so I had to start from scratch.

At this time I reached my breaking point with Windows and decided to dump it and buy myself a Mac (even though I had never before seen one) and found that delphi doesn't exist on the Mac. C++ was now mandatory, only on the Mac they use Obj-C but it is still fully compatible with C++.

The publishing house I intend to use say they will only release titles that are cross platform between Mac and Win because 75% of their sales are on the Mac. So even using Delphi in Virtual PC is not an option.

I now write the graphical side of the game on my Mac when I'm at home and use my wife's Windows system to code all other generic code in Studio.Net to transfer to the Mac when I get home at the end of the week.

<back on topic>now to the point i am tring to make<back on topic>
I am busy re creating the game I had made way back then so already have a clear view of what has to be done. First thing first, rewrite my speach parser to create dialogue. So far all the code i write work perfectly and i succesfully managed to read in a text file (although using fopen() and reading in 1 char at a time) and send the combined text line to the line parser, which then successfully splits the line into it's separate components and sends each one off to the corrent validation functions... but then I can't insert the data into the main speach class because the data goes into a linked list created and stored as .h and .cpp files that i can't link back in...

In asnwer to your question, yes, i only define the class and methods in the .h file and do all coding of the methods in the .cpp file

Shoot. You're doing template stuff. I think you may need the code in the header. (Kind of a new area for me, maybe someone else will confirm this while I continue looking into it -- I haven't followed all of the steps you described yet.)

#pragma once

is this an alternate way of doing what i do or is it the new standard?

I believe it's Microsoft's nonstandard way to do what you've seen done a standard way.

I took a few screen caps last night but my windows system doesn't have net access so i will connect it to my router when i get home tonight (at class today)

http://imageshack.us

I noticed you use <iostream>
i am trying to make my code completely generic and not dependent on any namespace or libs that might not be available on another platform. for this reason i just use the stdafx header that gets included when i create the console app.

Using stdafx is an annoying Microsoft thing -- so doing so is at odds with your stated goal. And <iostream> is part of C++. So is the std namespace.

[Still parsing your reply...]

well, i still don't know why i get linking errors using templates but i read up about it and found that , as you said namespace std is standard now across all platforms, there is no need for me to build my own templates so i dumped all my template code and replaced it with 'list' and rewrote my code to use 'list' as opposed to my templates and now everything works like a dream!!!!!!

Thanks for that info!!!!

Tje only real difference between my old code and new code is this
OLD:

if (var) do
{
//process this var
...
//continue
var = var->next;
}while (var);

and

var->insert(newVar);

NOW:

if (!var->empty())
for (list<keyval>::iterator it = var->begin(); it != var->end(); it++)
{
}

and

var->push_back(newvar);

<< moderator edit: added [code][/code] tags >>

As you can see it didn't require much change but it really dit make life a whole lot easier...

Thanks for the info. You have been a great help!

Now, just for interrest sake, if someone would just be able to explain WHY my templates didn't work I would be much obliged. Should I have just included the source with the defs in the same file? Is that it?

Anyway, my first project in c++, a full fledged speachfile parser has been completed and works perfectly... now onto the next hurdle. But do you realise what I am saying? Let me rephrase:

"I can now code in c++! Yeah!!!"
Thanks tons :lol: :p :cry: :D :D :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.