【问题标题】:Icomplete implementation and Pointer conversion error.Icomplete 实现和指针转换错误。
【发布时间】:2012-02-19 16:13:25
【问题描述】:

编译器警告(1):不完整的实现

我已经比较了我的 .h 和 .m 文件,以查看声明和实现的内容之间是否存在任何不一致或拼写错误,但找不到任何内容。

编译器警告(2):不兼容的整数到指针转换发送“NSInteger*”(又名“int*”)和“int”类型的表达式。

25 分钟以来,我一直在用各种组合的星号搞砸,编译器仍然不满意。

#import "Game.h"
#import "stdlib.h"

const int MAXRAND = 15;
const int MAXCOL = 7;
const int MAXROW = 9;

NSInteger gameState[MAXROW][MAXCOL];
NSInteger answerBoard[MAXROW][MAXCOL];

@implementation Game//compiler warning 1


-(void)init:(NSInteger*) rows: (NSInteger*) columns: (NSInteger*) operators:(NSInteger*) operands{
    NSLog(@"init sent");
    numRows = *rows;
    numColumns = *columns;
    numOperators = *operators;
    numOperands = *operands;
    //seed random number generator

    //generate rand nums for operands
    int operandList[numOperands];
    for (int i = 0; i < numOperands; i++) {
        srandom(time(NULL));
        operandList[i] = (random()%MAXRAND);
    }
    //generate state and answer board
    BOOL gameState[numRows][numColumns];
    NSInteger answerBoard[numRows][numColumns];    
    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numColumns; j++) {
            gameState[i][j] = NO;
            answerBoard[i][j] = (operandList[random()%numOperands])+
            (operandList[random()%numOperands])-
            (operandList[random()%numOperands]);
        }

    }
}

-(void)updateGame:(NSInteger*)enteredNum{
    NSLog(@"updateGame sent");
    for (int i = numColumns; i > 0; i--) {
        for (int j = numRows; j > 0; j--) {
            if (gameState[i][j] == NO){
                if (*enteredNum == answerBoard[i][j]){
                    gameState[i][j] = YES;
                }
            }

        }
    }
}


@end//Game

#import <Foundation/Foundation.h>

@interface Game : NSObject
{
    NSInteger numRows, numColumns, numOperators, numOperands;
}

-(void)init:(NSInteger*) rows: (NSInteger*) columns: (NSInteger*) operators:(NSInteger*) operands;
-(void)updateGame:(NSInteger*) enteredNum;

@end

我的类的实例在哪里声明和初始化:

 NSInteger *rows = 7, *columns = 6, *operators = 2, *operands = 6;//compiler warning 2
 Game *game = [Game new];
 [game init:rows :columns :operators :operands];

【问题讨论】:

  • 真诚地,您需要对C编程语言的基本点进行复习。
  • 我试图在一个测试项目中编译你的代码并且它正在工作..尝试清除你的项目

标签: objective-c ios5 compiler-warnings


【解决方案1】:
NSInteger *rows = 7, *columns = 6, *operators = 2, *operands = 6;

rows,columns, operators, operands 的类型为 NSInteger *。您需要分配内存,然后需要将 7,6,2,6 放在它们指向的内存位置。在 C 术语中,

int *ptr = 7; // Wrong. Because, ptr is pointing no where to keep 7 in that location.

【讨论】:

  • 我最初没有星号,但出现了另一个错误。你能建议一种替代方法或写它吗?我有点困惑。
【解决方案2】:

你试过了吗?

NSInteger rows = 7, columns = 6, operators = 2, operands = 6;

那是什么?

[Game new];

编译器可能希望您实现一个名为 new 的函数。

编辑: 尝试从您的 NSInteger 中删除所有“*”。

【讨论】:

  • "new" 只是一种说法 [[Game alloc] init] stackoverflow.com/questions/719877/…
  • 对不起,我不知道“新”。我总是使用“alloc”和“init”。
  • 我确实删除了星号,根据方法定义和 Obj-C 语法,我必须接受一个指针,即通过引用传递。否则我会得到一个错误。所以我需要的是:NSInteger 的声明和初始化,然后是一个指向它的指针。
猜你喜欢
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2016-05-14
  • 2017-07-27
  • 1970-01-01
  • 2016-09-13
相关资源
最近更新 更多