如何解决“ - >'”的无效类型参数和“期望标识符或'(''''''令牌'错误?

问题描述 投票:0回答:2
 #include "int_set.h" 


  Set * int_set_new()
   {
    Set *result; 

result-> current_elements = 0; 
result-> max_elements = MIN_SIZE; 
result->array =malloc(sizeof(int) * MIN_SIZE);
return result;
  }
   int int_set_lookup(Set*this, int item)
  {
  int i = 0;
int result;
int found = 0 ;
for ( int i = 0; i<this->max_elements && found ==0 ; i++){
    //if (result->array[i] == item ) {
    if(result->array[i]== item){ // error invalid type of argument'->'

        found = 1; 
    } 
}
    if ( i>= this-> current_elements  ){
        found*= -1 ; 
    }  
    return found ;
} 

 int int_set_add(Set*this, int item){


if ( int_set_lookup(this, item) == 1){
    return 0;
}
if ( this-> current_elements < this -> max_elements  ){
    this -> array[this->current_elements] = item ; 
    this->current_elements++ ; 
    return 1 ;
}else{
    int i;
    int new_array[i];

    int [] new_array = malloc(sizeof(int)* 1.5*this->current_elements); // error
    for ( i=0; i<this->max_elements ; i++){
        new_array[i] = this->array[i];

    }
    this-> max_elements = 1.5*this->current_elements; 

    new_array[this->current_elements] = item;
    this-> current_elements ++;

    free ((void *)this->array); 

    this->array = new_array ; 
    return 1;
}

}

   int int_set_remove(Set * this, int item){
if ( int_set_lookup(this, item) == 0){
    return 0;
}
int i = 0;
int array[i];

for( i ; i< this-> current_elements && this->array != item; i++);

for( i; i< this-> current_elements-1;  i++){
    array[i] = array [i+1];
}
this-> current_elements-- ; 

int freeSlots= this->max_elements-this->current_elements;
int maxFree= 32> this->current_elements? 16 : this->current_elements/2 ;
if( freeSlots > maxFree ){

    int new_array[i];
    //int [] new_array = malloc(sizeof(int)* 1.5*this->current_elements);
    int [] new_array = malloc(sizeof(int)* 1.5*this->current_elements); // error
    for ( i=0; i<this->current_elements ; i++){
        new_array[i] = this->array[i];

    }
    this-> max_elements = 1.5*this->current_elements; 



    free ((void *)this->array); 

    this->array = new_array ; 
    }

   return 1;
     }

//error的2行

预期标识符或'(''''标记错误之前

如何解决上述错误?

有问题的线是

if(result->array[i]== item){ // error invalid type of argument'->'

int [] new_array = malloc(sizeof(int)* 1.5*this->current_elements); // error

请帮忙!

c arguments identifier
2个回答
0
投票
  1. result是一个int,而不是指针所以你不能使用->。不是说你无论如何都可以使用int *,因为它可以引用一个struct *
  2. 你已经宣布new_array所以不需要再做一次。加上int [] new_array在C语言中是不正确的语法,与Java不同。

0
投票

result是一个局部变量,所以在int_set_new()它是一个Set*但在int_set_lookup()它是一个int。如果你将它改为int*然后它指向一个int,而不是一个struct,那么如何用->引用一个int的字段

对于第二个错误,this是C ++中的保留字。也许你正在编写错误的语言

顺便说一句,你应该缩进你的代码

© www.soinside.com 2019 - 2024. All rights reserved.