gpt4 book ai didi

compiler-errors - 如何定义指向相同名称的静态类方法的全局宏?

转载 作者:行者123 更新时间:2023-12-02 10:54:06 25 4
gpt4 key购买 nike

我有以下MQL4/5代码:

class MQL4 {
public:
static double Ask() {
MqlTick _tick;
SymbolInfoTick(_Symbol, _tick);
return _tick.ask;
// Overriding Ask variable to become a function call.
#define Ask MQL4::Ask()
}

};

void start() {
double ask = Ask; // line 14
};

但是,根据错误,它无法在MQL4或MQL5下编译:
> mql /s /mql5 Test.mqh
MQL4/MQL5 Compiler build 1162 (02 Jul 2015)
Test.mqh : information: Checking 'Test.mqh'
Test.mqh(14,16) : error 320: 'Ask' - too complex, simplify the macro
Test.mqh(14,16) : error 239: '::' - syntax error
Test.mqh(14,16) : error 239: '::' - syntax error
Test.mqh(14,16) : error 239: '::' - syntax error
Test.mqh(14,16) : error 239: '::' - syntax error
Test.mqh(14,16) : error 239: '::' - syntax error
Test.mqh(14,16) : error 239: '::' - syntax error
Test.mqh(14,16) : error 239: '::' - syntax error
Test.mqh(14,16) : error 149: unexpected token
Test.mqh(14,16) : error 149: ')' - unexpected token
Test.mqh(14,16) : error 157: 'MQL4' - expression expected
Test.mqh(14,10) : warning 31: variable 'ask' not used
: information: Result 11 error(s), 1 warning(s)

与最新的1498版本相同的错误。

基本上是说 Ask宏太复杂了。虽然在将 Ask() 方法重命名为 GetAsk() 并更新宏定义时效果很好,但是我想了解是否有其他解决方案而不必重命名。

有什么我可以定义 macro substitution的语法,它可以理解以下宏:
#define Ask MQL4::Ask()

无需重命名,同时仍将其保留在静态类方法中?

最佳答案

是的,但是……,不! #define替换可以正常工作,但是...但是 Ask 是保留字,这会使编译器感到困惑。

实际上,问题出在编译器预处理器的功能有限,而不是 .method() 的“相同名称”。

非参数和参数替代功能 #define 指令均已测试到已发布的MQL4/5语言语法的极限。
Case[6]最终证明,
Class.<_method_>() 可以具有相同的名称 .Ask() ,但不能与 #define -s <_literal_symbol_to_substitute_>相同

//+------------------------------------------------------------------+
//| StackOverflow__test_DEFINE.mq4 |
//| Copyright © 1987-2017 [ME] |
//| nowhere.no |
//+------------------------------------------------------------------+
#property strict
// -------------------------------------------------------------------------------------------------------------------------
/* // [Case[1] FAILS BELOW
#define Ask MQL4::Ask( True ) // [Case[1] // Overriding Ask variable to become a function call. */

class MQL4 {
public:
static double Ask( bool FakeSyntaxSUGAR = False ) { // [Case[1] FAILS HERE TO PROCESS EXPANDED PRE-PROCESSOR #define SUBSTITUTION]
MqlTick _tick;
SymbolInfoTick( _Symbol,
_tick
);
return _tick.ask;
}
};

//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+

void OnStart() {

// -------------------------------------------------------------------------------------------------------------------------
/* // [Case[2] FAILS
#define Ask MQL4::Ask( True ) // [Case[2] // Overriding Ask variable to become a function call. */
// double ask = Ask; // [Case[2] FAILS TO COMPILE, ERROR on "Ask;"]


// ------------------------------------------------------------------------------------------------------------------------
// double askMQL4 = MQL4::Ask( True ); // [Case[3] OK TO COMPILE EXPLICIT CALL TO an explicit absolute reference to a method-name]


// -------------------------------------------------------------------------------------------------------------------------
/* // [Case[4] OK */
MQL4* aPtrToINSTANCE = new MQL4(); // [Case[4]
// double askINST = aPtrToINSTANCE.Ask( False ); // [Case[4] OK TO COMPILE EXPLICIT CALL TO aPtrToINSTANCE-referred instance of Class ]

// -------------------------------------------------------------------------------------------------------------------------
/* // [Case[5] FAILS
#define Ask aPtrToINSTANCE.Ask( False ) // [Case[5] */
// double askINST = Ask; // [Case[5] FAILS TO PRE-PROCESS #define HERE ]


// -------------------------------------------------------------------------------------------------------------------------
/* // [Case[6] OK */
#define Ask_with_SomeOtherDEFINE_NAME aPtrToINSTANCE.Ask( False )
double askINST = Ask_with_SomeOtherDEFINE_NAME; // [Case[6] OK TO CALL THE .Ask() METHOD AS THE PRE-PROCESSOR <_SYMBOL_TO_SUBSTITE_> WAS #define-d AS NON-COLLIDING]

// ** * **** **** ***** *****
// *** * ** * ** * ** ** *
// ** * * ** * ** * *** ** *
// ** ** ** * ** * * ** ** *
// ** * **** ** **** ** ***** ** ***** **
// * *
};
// ------------------------------------------------------------------

结语:

As I have posted somewhere in some other MQL4-posts, after having some extensive testing of the capabilities available from the #define directive based pre-compiler substitutions ( for version management purposes et al ), I could but draw a conclusion to try to avoid using this feature, as the resulting problems were far more expensive ( surprising string-handling limitations, uncertainties about the continuing language creeps, unpredictable results of syntax-sugar tricks after next Build releases etc. ) than beneficial for the purposes intended.

关于compiler-errors - 如何定义指向相同名称的静态类方法的全局宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41699314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com