Delphi Generics:E2037:'XXX'的声明与之前的声明不同

İbr*_*him 1 c++ delphi generics

我想将我的C++代码转换为Delphi代码.但我从Delphi编译器得到这个错误:Declaration of 'callFunction<T>' differs from previous declaration.
我的C++代码:

class Example
{
public:
  template<typename T>
  static void callFunction(const T value);
};

template<>
void Example::callFunction<int>(const int value)
{
  cout << "Integer = " << value << endl;
}

template<>
void Example::callFunction<double>(const double value)
{
  cout << "Double = " << value << endl;
}

template<>
void Example::callFunction<char*>(char* const value)
{
  cout << "Char* = " << value << endl;
}

int main()
{
  Example::callFunction<int>(17);
  Example::callFunction<double>(3.8);
  Example::callFunction<char*>("Hello");

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码成功运行.但是我的Object Pascal代码没有运行.
我的Delphi代码:

type
  Example = class
  public
    class procedure callFunction<T>(const val: T);
  end;

{ Example }

class procedure Example.callFunction<Integer>(const val: Integer);
begin
  Writeln('Integer');
end;

class procedure Example.callFunction<Double>(const val: Double);
begin
  Writeln('Double');
end;

class procedure Example.callFunction<PChar>(const val: PChar);
begin
  Writeln('PChar');
end;

begin
  Example.callFunction<Integer>(17);
  Example.callFunction<Double>(3.8);
  Example.callFunction<PChar>('Hello');

  Readln;
end.
Run Code Online (Sandbox Code Playgroud)

如何将我的C++代码转换为Delphi代码?错误的原因是什么?我可以像这样将代码转换为Delphi吗?谢谢.

Dsm*_*Dsm 5

我认为你误解了仿制药.关于泛型的全部观点是你没有在类定义中明确使用类型,所以像

class procedure Example.callFunction<Integer>(const val: Integer);
Run Code Online (Sandbox Code Playgroud)

不合法.相反,在这种情况下,你不会使用泛型,而是像这样重载函数.

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.
Run Code Online (Sandbox Code Playgroud)

请注意,我使用的是字符串而不是PChar,因为这更有可能是您需要的.