【问题标题】:Received an "expected expression error " when i added a function添加函数时收到“预期的表达式错误”
【发布时间】:2020-07-12 03:00:30
【问题描述】:

我正在尝试编写一个无法编译的程序。我不断收到的错误是这样的

预期的表达 destroyFallingStone (int map[][SIZE], int column);

它发生在我添加了 destroyFallingStone 函数并且我检查了函数和函数原型是否有任何语法错误之后。我不知道我在哪里犯了错误。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define SIZE 15
#define EMPTY 0
#define STONE 1

// TODO: Add any extra #defines here.

// TODO: Add any extra function prototypes here.

void printMap(int map[SIZE][SIZE], int playerX);

void destroyFallingStone (int map[][SIZE], int column);

int main (void) {
    // This line creates our 2D array called "map" and sets all
    // of the blocks in the map to EMPTY.
    int map[SIZE][SIZE] = {EMPTY};

    // This line creates out playerX variable. The player starts in the
    // middle of the map, at position 7.
    int playerX = SIZE / 2;

    printf("How many lines of stone? ");
    // TODO: Scan in the number of lines of blocks.
    int linesOfStone; 
    scanf("%d", &linesOfStone);

    printf("Enter lines of stone:\n");
    // TODO: Scan in the lines of blocks.
    
    int rowPos; 
    int columnPos; 
    int stoneLength; 
    int stoneValue; 
   
    int i = 0; 
    while (i < linesOfStone) {
        scanf("%d %d %d %d", &rowPos, &columnPos, &stoneLength, &stoneValue);
        
        if ( 0 <= rowPos && rowPos < SIZE && 
             0 <= columnPos && columnPos < SIZE
             && columnPos + stoneLength - 1 < SIZE) { 
            int j = 0; 
            while (j < stoneLength) { 
                map[rowPos][columnPos + j] = STONE; 
                j++; 
            }
        }
        i++; 
    }

    printMap(map, playerX);

    // TODO: Scan in commands until EOF.
    // After each command is processed, you should call printMap.
    
  
    int quitLoop = 0; 
    int playerDirection = 0; 
    int playerMovement = 0; 
    
    while (quitLoop != 1) {
    scanf("%d %d", &playerMovement, &playerDirection); 
        if ( playerMovement == 1 && 
            playerDirection == 1 && playerX < (SIZE - 1)) { 
        //check player is within bounds 
        playerX++; 
        } else if ( playerMovement == 1 && 
                   playerDirection == -1 && playerX > 0 ) {
        playerX--;
        } else if ( playerMovement == 2) { // call function for destroying stones 
            destroyFallingStone (int map[][SIZE], int column);
        }
    
    printMap(map, playerX);
    } 
   

    return 0;
}

// Print out the contents of the map array. Then print out the player line
// which will depends on the playerX variable.
void printMap(int map[SIZE][SIZE], int playerX) {
    
    // Print values from the map array.
    int i = 0;
    while (i < SIZE) {
        int j = 0;
        while (j < SIZE) {
            printf("%d ", map[i][j]);
            j++;
        }
        printf("\n");
        i++;
    }    
    
    // Print the player line.
    i = 0;
    while (i < playerX) {
        printf("  ");
        i++;
    }
    printf("P\n");
}

//destroys the closes 2 stones 
void destroyFallingStone (int map[][SIZE], int column) {

    int i = 0;  
    int j = 0; 

    while (j < 3) {
        while (i < 15 && map[i][column] != STONE) { //finding the first stone 
            i++; 
        }
        // if there is a stone, destroy it 
        if (map[i][column] == STONE) {
            map[i][column] = EMPTY;
        }    
        i++; 
    }
}








【问题讨论】:

  • 调用函数的地方,不需要类型:destroyFallingStone (int map[][SIZE], int column); => destroyFallingStone (map, column);(类似于你调用printMap的方式)。

标签: c


【解决方案1】:

你没有正确调用函数:

    } else if ( playerMovement == 2) { // call function for destroying stones 
        destroyFallingStone (int map[][SIZE], int column);
    }

你在这里看到的更像是一个声明而不是一个函数调用。你反而想要:

    } else if ( playerMovement == 2) { // call function for destroying stones 
        destroyFallingStone(map,column);
    }

【讨论】:

    【解决方案2】:

    问题在于:75 destroyFallingStone (int map[][SIZE], int column);。这应该是对函数destroyFallingStone 的调用。类似destroyFallingStone (map, columnPos);

    以下是更正的代码。见it compiling here

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define SIZE 15
    #define EMPTY 0
    #define STONE 1
    
    // TODO: Add any extra #defines here.
    
    // TODO: Add any extra function prototypes here.
    
    void printMap(int map[SIZE][SIZE], int playerX);
    
    void destroyFallingStone (int map[][SIZE], int column);
    
    int main (void) {
        // This line creates our 2D array called "map" and sets all
        // of the blocks in the map to EMPTY.
        int map[SIZE][SIZE] = {EMPTY};
    
        // This line creates out playerX variable. The player starts in the
        // middle of the map, at position 7.
        int playerX = SIZE / 2;
    
        printf("How many lines of stone? ");
        // TODO: Scan in the number of lines of blocks.
        int linesOfStone; 
        scanf("%d", &linesOfStone);
    
        printf("Enter lines of stone:\n");
        // TODO: Scan in the lines of blocks.
        
        int rowPos; 
        int columnPos; 
        int stoneLength; 
        int stoneValue; 
       
        int i = 0; 
        while (i < linesOfStone) {
            scanf("%d %d %d %d", &rowPos, &columnPos, &stoneLength, &stoneValue);
            
            if ( 0 <= rowPos && rowPos < SIZE && 
                 0 <= columnPos && columnPos < SIZE
                 && columnPos + stoneLength - 1 < SIZE) { 
                int j = 0; 
                while (j < stoneLength) { 
                    map[rowPos][columnPos + j] = STONE; 
                    j++; 
                }
            }
            i++; 
        }
    
        printMap(map, playerX);
    
        // TODO: Scan in commands until EOF.
        // After each command is processed, you should call printMap.
        
      
        int quitLoop = 0; 
        int playerDirection = 0; 
        int playerMovement = 0; 
        
        while (quitLoop != 1) {
        scanf("%d %d", &playerMovement, &playerDirection); 
            if ( playerMovement == 1 && 
                playerDirection == 1 && playerX < (SIZE - 1)) { 
            //check player is within bounds 
            playerX++; 
            } else if ( playerMovement == 1 && 
                       playerDirection == -1 && playerX > 0 ) {
            playerX--;
            } else if ( playerMovement == 2) { // call function for destroying stones 
                destroyFallingStone (map, columnPos);
            }
        
        printMap(map, playerX);
        } 
       
    
        return 0;
    }
    
    // Print out the contents of the map array. Then print out the player line
    // which will depends on the playerX variable.
    void printMap(int map[SIZE][SIZE], int playerX) {
        
        // Print values from the map array.
        int i = 0;
        while (i < SIZE) {
            int j = 0;
            while (j < SIZE) {
                printf("%d ", map[i][j]);
                j++;
            }
            printf("\n");
            i++;
        }    
        
        // Print the player line.
        i = 0;
        while (i < playerX) {
            printf("  ");
            i++;
        }
        printf("P\n");
    }
    
    //destroys the closes 2 stones 
    void destroyFallingStone (int map[][SIZE], int column) {
    
        int i = 0;  
        int j = 0; 
    
        while (j < 3) {
            while (i < 15 && map[i][column] != STONE) { //finding the first stone 
                i++; 
            }
            // if there is a stone, destroy it 
            if (map[i][column] == STONE) {
                map[i][column] = EMPTY;
            }    
            i++; 
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多