Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在类语法帮助中比较对象; 使用C++比目标C更自信,我只是想尝试比较两个对象(我正在构建的单字类)。我不断收到错误, 期待一种类型;这是一个基本的问题,但我也想解释一下我是怎么做的,这是我在C++中写的。很好用 private: vector <int> LineNumbers; string wordCatalog;// the current word in the line number int newIndex(const int); public: UniqueWord(const string,const int);//creates the unique word object ~UniqueWord(void); void addLine(const int); static int compare(const UniqueWord&, const UniqueWord&);//my issue here string toString() const;_C++_Objective C_Class_Object - Fatal编程技术网

在类语法帮助中比较对象; 使用C++比目标C更自信,我只是想尝试比较两个对象(我正在构建的单字类)。我不断收到错误, 期待一种类型;这是一个基本的问题,但我也想解释一下我是怎么做的,这是我在C++中写的。很好用 private: vector <int> LineNumbers; string wordCatalog;// the current word in the line number int newIndex(const int); public: UniqueWord(const string,const int);//creates the unique word object ~UniqueWord(void); void addLine(const int); static int compare(const UniqueWord&, const UniqueWord&);//my issue here string toString() const;

在类语法帮助中比较对象; 使用C++比目标C更自信,我只是想尝试比较两个对象(我正在构建的单字类)。我不断收到错误, 期待一种类型;这是一个基本的问题,但我也想解释一下我是怎么做的,这是我在C++中写的。很好用 private: vector <int> LineNumbers; string wordCatalog;// the current word in the line number int newIndex(const int); public: UniqueWord(const string,const int);//creates the unique word object ~UniqueWord(void); void addLine(const int); static int compare(const UniqueWord&, const UniqueWord&);//my issue here string toString() const;,c++,objective-c,class,object,C++,Objective C,Class,Object,我将非常感谢解释基本的语法规则(用现代语言解释),这样下次我就不会有这个麻烦了,谢谢。同样,我对目标C不是很有信心 顺便问一句,有没有人能告诉我,我的单字构造器是否正确?在Objective-C中没有静态的方法,你需要一个类方法。将声明前面的-替换为+,如下所示: +(NSInteger) compare:(UniqueWord *self)a with:(UniqueWord *self)b; -(id)initWithString:(NSString*)str andIndex:(NSIn

我将非常感谢解释基本的语法规则(用现代语言解释),这样下次我就不会有这个麻烦了,谢谢。同样,我对目标C不是很有信心


顺便问一句,有没有人能告诉我,我的单字构造器是否正确?在Objective-C中没有静态的方法,你需要一个类方法。将声明前面的
-
替换为
+
,如下所示:

+(NSInteger) compare:(UniqueWord *self)a with:(UniqueWord *self)b;
-(id)initWithString:(NSString*)str andIndex:(NSInteger)index;
+(id)wordWithString:(NSString*)str andIndex:(NSInteger)index;

类方法类似于C++静态成员函数,但是由于方法调度在Objtovi-C中更为动态地实现,所以可以在派生类中为它们提供重写。 以上内容将被编译。但是,这对于Objective-C来说并不惯用,因为Cocoa使用而不是

NSInteger
作为比较方法的返回类型:

+(NSComparisonResult) compare:(UniqueWord *self)a with:(UniqueWord *self)b;

此外,C++的构造函数是通过指定的初始化器实现的: 应该是这样的:

+(NSInteger) compare:(UniqueWord *self)a with:(UniqueWord *self)b;
-(id)initWithString:(NSString*)str andIndex:(NSInteger)index;
+(id)wordWithString:(NSString*)str andIndex:(NSInteger)index;
和/或类似于:

+(NSInteger) compare:(UniqueWord *self)a with:(UniqueWord *self)b;
-(id)initWithString:(NSString*)str andIndex:(NSInteger)index;
+(id)wordWithString:(NSString*)str andIndex:(NSInteger)index;

Objective-C中没有
静态
方法-您需要一个类方法。将声明前面的
-
替换为
+
,如下所示:

+(NSInteger) compare:(UniqueWord *self)a with:(UniqueWord *self)b;
-(id)initWithString:(NSString*)str andIndex:(NSInteger)index;
+(id)wordWithString:(NSString*)str andIndex:(NSInteger)index;

类方法类似于C++静态成员函数,但是由于方法调度在Objtovi-C中更为动态地实现,所以可以在派生类中为它们提供重写。 以上内容将被编译。但是,这对于Objective-C来说并不惯用,因为Cocoa使用而不是

NSInteger
作为比较方法的返回类型:

+(NSComparisonResult) compare:(UniqueWord *self)a with:(UniqueWord *self)b;

此外,C++的构造函数是通过指定的初始化器实现的: 应该是这样的:

+(NSInteger) compare:(UniqueWord *self)a with:(UniqueWord *self)b;
-(id)initWithString:(NSString*)str andIndex:(NSInteger)index;
+(id)wordWithString:(NSString*)str andIndex:(NSInteger)index;
和/或类似于:

+(NSInteger) compare:(UniqueWord *self)a with:(UniqueWord *self)b;
-(id)initWithString:(NSString*)str andIndex:(NSInteger)index;
+(id)wordWithString:(NSString*)str andIndex:(NSInteger)index;

我认为更好的建议是实现一个实例
compare:
方法

有很好的理由这样做,特别是,接下来要使用新的
compare:
方法对一组独特的单词进行排序。@dasblinkenlight建议的类方法(语法无可挑剔)将迫使您编写自己的排序。一个实例
compare:
提供了紧凑且可能更高效的替代方案:

[myArrayFullOfUniqueWords sortUsingSelector:@selector(compare:)];

我认为更好的建议是实现一个实例
compare:
方法

有很好的理由这样做,特别是,接下来要使用新的
compare:
方法对一组独特的单词进行排序。@dasblinkenlight建议的类方法(语法无可挑剔)将迫使您编写自己的排序。一个实例
compare:
提供了紧凑且可能更高效的替代方案:

[myArrayFullOfUniqueWords sortUsingSelector:@selector(compare:)];

你能给我解释一下+号而不是-请?也许吧,但这不是实现实例相等和散列的更好答案吗?还有静态的替代方法吗?@OPJ在Objective-C中减去
-
表示“实例方法”(C++中的非静态成员函数),加上
++
表示“类方法”(与C++中的静态成员函数紧密对应,但不是完全一致)在C++ C++中,可以使用C++中的静态方法,但是不能像C++那样定义静态数据成员,而是定义一个类方法,并使它从函数内或从内部返回一个<代码>静态<代码>对象。心理状态文件。你能给我解释一下+号而不是-请?也许吧,但这不是实现实例相等和散列的更好答案吗?是否有静态的替代物?@OPJ在Objective-C中减去
-
表示“实例方法”(C++中的非静态成员函数),加上
+
表示“类方法”(与C++中的静态成员函数紧密对应,但不是完全一致)在C++ C++中,可以使用C++中的静态方法,但是不能像C++那样定义静态数据成员,而是定义一个类方法,并使它从函数内或从内部返回一个<代码>静态<代码>对象。心理状态文件。