Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (VoroneTZ, monk12, Quad), 829 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Using STL in Zorro strategies #481505
09/22/20 12:50
09/22/20 12:50
Joined: Jul 2017
Posts: 783
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 783
I needed to use an unordered_map<> in my c++ code.

However, just adding #include <unordered_map> in the header file with the definition of the relevant class (even without using this container) generates numerous compiler errors.

Including it in pch.h kinda works, but shows a conflict around 'string'

C:\Users\Administrator\source\repos\EA_MasterPortfolioCPP\EA_Vars.h(147,1): error C2872: 'string': ambiguous symbol
1>C:\Zorro\include\trading.h(13,15): message : could be 'char *string'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\xstring(4564,7): message : or 'std::string'


Which concerns not only my script but the whole of Zorro framework which relies upon typedef char* string; in trading.h. (e.g. string sExchange, etc)

Wouldn't it be better to change this typedef to a different name (for c++)?

If not, what's the workaround?

Last edited by Zheka; 09/22/20 13:36.
Re: Using STL in Zorro strategies [Re: Zheka] #481507
09/22/20 15:10
09/22/20 15:10
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Works for me.

The order of including headers is important. First, include your STL headers, then include the zorro header last.

Re: Using STL in Zorro strategies [Re: Zheka] #481508
09/22/20 16:38
09/22/20 16:38
Joined: Jul 2017
Posts: 783
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 783
If I include <unordered_map> in ZorroDLL.cpp after pch.h and before zorro.h - all indeed compiles fine and there is no conflict with 'string'.

However, attempting to define an unordered_map <string, int> variable later shows that unordered_map is not recognized as a type by the compiler.

Where is the catch?

Re: Using STL in Zorro strategies [Re: Zheka] #481509
09/22/20 17:01
09/22/20 17:01
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Again, this works for me, except I did not touch the precompiled headers. Try keeping that stuff out of the precompiled headers.

More importantly, it does not seem like a good idea to use a char pointer as a key in a map, as this can have some unexpected side effects.

Re: Using STL in Zorro strategies [Re: Zheka] #481511
09/23/20 10:30
09/23/20 10:30
Joined: Jul 2017
Posts: 783
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 783
If the intention is to use an Asset as a key, what would you use and why?

Re: Using STL in Zorro strategies [Re: Zheka] #481512
09/23/20 12:49
09/23/20 12:49
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Probably std::string. It has built-in content comparison operators. (In other words, it does strcmp() for you.)

Whereas if you use char*, you compare by pointer address value. The same string contents can exist at multiple addresses, so you can end up with duplicate entries for the same string content.

EDIT: Actually, you can use char* correctly in std::map, but you have to supply a comparison functor:
https://stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap/4157729#4157729

Also, you would still have to guarantee that the pointers are valid. At least with std::string, each string maintains its own copy of its full string. For that reason, you'd still be safer with std::string. (There are more optimized implementations available, but I will not discuss that here.)

Last edited by AndrewAMD; 09/23/20 15:24.
Re: Using STL in Zorro strategies [Re: Zheka] #481518
09/24/20 10:37
09/24/20 10:37
Joined: Jul 2017
Posts: 783
Z
Zheka Offline OP
User
Zheka  Offline OP
User
Z

Joined: Jul 2017
Posts: 783
Clear, thank you. Will try.


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1