调用类方法时出现问题(问题:1)

发布于 2024-10-18 04:28:07 字数 838 浏览 5 评论 0原文

我正在尝试将包含一些静态方法的旧“C”程序转换为 Obj-c,但在编译它时遇到了一些问题。在头文件中,我有:

@interface Anneal : NSObject
...
...
+(float)approxInitT;
-(costType)simulatedAnnealing;
...

在实现文件中,有两个问题方法(为了简洁起见也进行了缩减):

@implementation Anneal

+(float)approxInitT
{
    float T=0.0;
    int m2=0;

    ...

    if(m2==0)
        T = T_LIFESAVER;
    else
        T = T / m2 / log(initProb);
    return T;
}

-(costType)simulatedAnnealing
{
    float T;

    ...

    if(Tset)
        T=initialT;
    else
        T=[self approxInitT]; // error:incompatible types in assignment
}

不幸的是,即使 'T' 和返回,我也收到“赋值中的不兼容类型”错误类方法都是“float”类型。虽然代码包含多个源文件(我预计在接下来的几天内会遇到更多问题),但它们都在同一个文件中。 这个问题显然是由我调用“approxInitT()”的方式中的错误引起的,但到目前为止,互联网搜索尚未发现我的问题的任何答案。

作为一名新手,我在多模型代码或使用静态/类方法方面没有任何经验,我肯定会感谢任何有关这方面的帮助。提前致谢 :-)

I'm trying to convert an old 'C' program containing some static methods into Obj-c but I'm having a few problems getting it to compile. In the header file I've got:

@interface Anneal : NSObject
...
...
+(float)approxInitT;
-(costType)simulatedAnnealing;
...

and in the implementation file, the two problem methods (also cut-down for brevity):

@implementation Anneal

+(float)approxInitT
{
    float T=0.0;
    int m2=0;

    ...

    if(m2==0)
        T = T_LIFESAVER;
    else
        T = T / m2 / log(initProb);
    return T;
}

-(costType)simulatedAnnealing
{
    float T;

    ...

    if(Tset)
        T=initialT;
    else
        T=[self approxInitT]; // error:incompatible types in assignment
}

Unfortunately I'm getting an "incompatible types in assignment" error even though 'T' and the return from the class method are both of type 'float'. While the code contains multiple source files (from which I'm expecting to hit a few more problems in the next few days), they're both in the same one.
The problem is obviously caused by an error in the way I'm calling 'approxInitT()' but a search of the internet hasn't uncovered any answers to my prob so far.

As a novice I don't have any experience in multi-model code OR using static/class methods, and I'd sure appreciate any help with this. Thanks in advance :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×眷恋的温暖 2024-10-25 04:28:08

self 引用特定类的实例,但当您调用类方法 (+approxInitT) 时,您必须将消息发送到您的类:T= [退火
近似初始化]

self references an instance of a particular class, but as you are calling a class method (+approxInitT), you must send the message to your class: T=[Anneal
approxInitT]

魂归处 2024-10-25 04:28:07

类方法不属于类的任何特定实例。因此,尝试将消息传递给类本身 -

T = [ Anneal approxInitT ];

Class methods donot belong to any particular instance of a class. So, try passing the message to class itself -

T = [ Anneal approxInitT ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文