当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《DelphiGenerics:E2037:'XXX'的声明与之前的声明不同》经验,为你挑选了1个好方法。

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

class Example
{
public:
  template
  static 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吗?谢谢.



1> Dsm..:

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

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,因为这更有可能是您需要的.

推荐阅读
臭小子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有