?
Solved

Delphi to C++

Posted on 2000-03-26
5
Medium Priority
?
333 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:PLC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2659963
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.
0
 
LVL 2

Expert Comment

by:Serega
ID: 2660073
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.
0
 

Author Comment

by:PLC
ID: 2660140
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)




0
 
LVL 3

Accepted Solution

by:
karouri earned 800 total points
ID: 2660664
The problem of global variables is that you want to declare it once aned include it many times.
The solution is to declare the variable as external in some header file, then declare it in a cpp file like this:

//globals.h
extern UINT myCounter;

//end of globals.h

//in globals.cpp
UINT myCounter;
//end of globals.cpp

These two files do the job,now you add globals.cpp to your project and include globals.h in any c file in your project.
Notes:
*C++ Builder uses this technique extensively,it treats a .h and .cpp file as one (Delphi-like unit)
*If I'm not wrong, this is what Serega actually meant, it is only a little more wordy here
*Why you use VC++ when every Delphiander that goes C uses C++ Builder?
0
 

Author Comment

by:PLC
ID: 2661003
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question