Link to home
Start Free TrialLog in
Avatar of PLC
PLC

asked on

Delphi to C++

I know this is a Delphi area, but I think only Delphi-people will be able to give me an explanation I can understand.
In my job someone had the bright idea to start using Visual C++ in stead of Delphi. I don't want to start a discussion on who is beter but since I started working in visual C++ I'm running in one stupid problem after the other. The most silly is this. How the hell do you declare a global variable. If I declare one, I get the compiler complaining the symbol is declared more than once. If I declare the global variable static it compiles, but creates a copy of the variable in the two classes I need the variable in. If I make it a member of a class, it complains about not being able to access a static member from a non-static function etc etc.
Whatever happened to putting
myGlobal: integer; in your main unit and being able to access it anywhere you want????
Do I really have to go follow courses to be able to learn visual C++.
Anyway, this should be an ease 200 points for anyone who know visual C++ too.
tnx
Patrick
Avatar of Epsylon
Epsylon

If you declare a variable in the main unit, you can't access it in any other unit unless you #include the main unit. When there is a circular #include reference you can get that 'symbol is declared more than once' message.

I suggest you drop that global variable in a file that's included in all units.
I suppose you need to declare variable in some unit, then set it in corresponding .h file as external and include that .h in any unit where you need.
Also I propose you to make a special unit for global variables.
Avatar of PLC

ASKER

Yep, that's what I tried to do finally. Sounded silly at the time, but I made a file called myGlobals.h with exactly 1 line in it:
UINT myCounter;
and included it in both the other units which let to these problems:

error LNK2005: "unsigned int  myCounter" (?myCounter@@3IA) already defined in ComputeIt.obj

or declared external
error LNK2001: unresolved external symbol "unsigned int  myCounter" (?myCounter@@3IA)




ASKER CERTIFIED SOLUTION
Avatar of karouri
karouri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PLC

ASKER

Tnx karouri, It's working. Amazing. After your answer, I do agree it was what Serega said but wordier, so also tnx to you Serega.
As for your question about C++builder. First of all, I see no reason not to stay with Delphi as I can do anything with that language. If we have to go to c++, my obvious choice would be C++ builder, but in a large corporation you get told VC++ and that's what it's going to be. So I'm trying to get grips with this awfull way of doing stuff.
You wouldn't happen to know a platform where I could ask these really stupid questions without bothering Delphi users here or VC++ experienced users elsewhere.
tnx again
Patrick