我想将我的C++代码转换为Delphi代码.但我从Delphi编译器得到这个错误:Declaration of 'callFunction
.
我的C++代码:
class Example { public: templatestatic void callFunction(const T value); }; template<> void Example::callFunction (const int value) { cout << "Integer = " << value << endl; } template<> void Example::callFunction (const double value) { cout << "Double = " << value << endl; } template<> void Example::callFunction (char* const value) { cout << "Char* = " << value << endl; } int main() { Example::callFunction (17); Example::callFunction (3.8); Example::callFunction ("Hello"); return 0; }
此代码成功运行.但是我的Object Pascal代码没有运行.
我的Delphi代码:
type Example = class public class procedure callFunction(const val: T); end; { Example } class procedure Example.callFunction (const val: Integer); begin Writeln('Integer'); end; class procedure Example.callFunction (const val: Double); begin Writeln('Double'); end; class procedure Example.callFunction (const val: PChar); begin Writeln('PChar'); end; begin Example.callFunction (17); Example.callFunction (3.8); Example.callFunction ('Hello'); Readln; end.
如何将我的C++代码转换为Delphi代码?错误的原因是什么?我可以像这样将代码转换为Delphi吗?谢谢.
我认为你误解了仿制药.关于泛型的全部观点是你没有在类定义中明确使用类型,所以像
class procedure Example.callFunction(const val: Integer);
不合法.相反,在这种情况下,你不会使用泛型,而是像这样重载函数.
type Example = class public class procedure callFunction(const val: integer); overload; class procedure callFunction(const val: double); overload; class procedure callFunction(const val: string); overload; end; { Example } class procedure Example.callFunction(const val: Integer); begin Writeln('Integer'); end; class procedure Example.callFunction(const val: Double); begin Writeln('Double'); end; class procedure Example.callFunction(const val: string); begin Writeln('string'); end; begin Example.callFunction(17); Example.callFunction(3.8); Example.callFunction('Hello'); Readln; end.
请注意,我使用的是字符串而不是PChar,因为这更有可能是您需要的.