2016-09-25 2 views
2

"ChessMoves.h"라는 헤더 파일과 다양한 함수가 포함 된 ChessMoves라는 파일이 있습니다. 내가헤더 파일에서 함수를 호출하는 데 도움이 필요합니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "ChessMoves.h" 

void parseNotationString(char *str, Move *whiteMove, Move *blackMove){ 

    int i, space = 0, j = 0, k = 0, l = 0; 
    int white[10], black[10], move[10], to[2]; 

    whiteMove.color = WHITE; 
    if(white[0] > 64) 
     whiteMove.piece = white[0]; 
    if(white[0] < 64) 
     whiteMove.from_loc.row = white[0]; 
    for(i = 0; i < 10; i++) 
     if(white[i] == 'x') 
      whiteMove.isCapture = 1; 
    for(i = 0; j < 10; i++) 
     if(white[i] == ' ') 
      to[0] = white[i-2]; 
      to[1] = white[i-1]; 

    printf("%c %c", to[0], to[0]); 
} 

에 전화 드렸습니다

헤더 파일

#ifndef __CHESSMOVES_H 
#define __CHESSMOVES_H 

typedef struct Location 
{ 
    // the square's column ('a' through 'h') 
    char col; 

    // the square's row (1 through 8) 
    int row; 
} Location; 

typedef struct Move 
{ 
    // location where this piece is moving from 
    Location from_loc; 

    // location where this piece is moving to 
    Location to_loc; 

    // what type of chess piece is being moved 
    char piece; 

    // whether this move captures another piece 
    short int isCapture; 

    // the color of the piece being moved 
    Color color; 
} Move; 

파일 우리는 코드를 테스트 할 파일을 받았다 해당 파일에서 그는이 있습니다

whiteMove.color != WHITE 

과 whiteMove.color가 WHITE와 같지 않으면 "FAIL"이라고 표시되어 시도했습니다. 설정

whiteMove.color = WHITE 

하지만 구조체 또는 공용체가 아닌 "구성원의 색상 요청"이 계속 나타납니다. 같은 것을 내가 호출하려고하는 다른 구조체에 대해 간다. 시도해 보았습니다.

Move.color = WHITE 

그리고 작동하지 않습니다.

+0

왜 초기화되지 않은'white [0]'을 테스트하고 있습니까? – Sergio

+0

세미콜론이없는 경우 C의 구문은 없습니다. – bmargulies

+0

whiteMove.color = 흰색; 해야합니다 whiteMove-> color; 그것은 포인터입니다. – bmargulies

답변

2

우리가 컴파일 할 수있는 것은이 파일을 모두 파일에 넣고 무의미한 비트를 잘라내어 누락 된 Color 열거 형을 추가 한 것입니다.

$ cat test.c 

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

typedef enum { WHITE, BLACK } Color; 

typedef struct Location 
{ 
    // the square's column ('a' through 'h') 
    char col; 

    // the square's row (1 through 8) 
    int row; 
} Location; 

typedef struct Move 
{ 
    // location where this piece is moving from 
    Location from_loc; 

    // location where this piece is moving to 
    Location to_loc; 

    // what type of chess piece is being moved 
    char piece; 

    // whether this move captures another piece 
    short int isCapture; 

    // the color of the piece being moved 
    Color color; 
} Move; 


void parseNotationString(char *str, Move *whiteMove, Move *blackMove){ 

    int i, space = 0, j = 0, k = 0, l = 0; 
    int white[10], black[10], move[10], to[2]; 

    whiteMove.color = WHITE; 

    if(white[0]>64) 
     whiteMove.piece = white[0]; 
    if(white[0]<64) 
     whiteMove.from_loc.row = white[0]; 
    for(i=0;i<10;i++) 
     if(white[i] == 'x') 
      whiteMove.isCapture = 1; 
    for(i=0;j<10;i++) 
     if(white[i] == ' ') 
      to[0] = white[i-2]; 
      to[1] = white[i-1]; 

    printf("%c %c", to[0], to[0]); 
} 

clang으로 컴파일하면 즉시 답변을 얻을 수 있습니다.

$ make 
cc -Wall -g test.c -o test 
test.c:40:14: error: member reference type 'Move *' (aka 'struct Move *') is a pointer; did you mean 
     to use '->'? 
    whiteMove.color = WHITE; 
    ~~~~~~~~~^ 
      -> 
test.c:43:18: error: member reference type 'Move *' (aka 'struct Move *') is a pointer; did you mean 
     to use '->'? 
     whiteMove.piece = white[0]; 
     ~~~~~~~~~^ 
       -> 
test.c:45:18: error: member reference type 'Move *' (aka 'struct Move *') is a pointer; did you mean 
     to use '->'? 
     whiteMove.from_loc.row = white[0]; 
     ~~~~~~~~~^ 
       -> 
test.c:48:22: error: member reference type 'Move *' (aka 'struct Move *') is a pointer; did you mean 
     to use '->'? 
      whiteMove.isCapture = 1; 
      ~~~~~~~~~^ 
        -> 
4 errors generated. 
make: *** [test] Error 1 

whiteMoveMove *하는 Move 구조에 대한 포인터이다. 따라서 ->으로 참조 해제되어야합니다. .은 직접 액세스 할 수 있습니다.

clang의 오류 메시지는 우수하며 해결 방법을 제안하기도합니다. 코드가 미묘한 버그가 또한


C.

를 배우면서 나는 강하게 당신이 그것을, 또는 유사 좋은 오류가있는 컴파일러를 사용하는 것이 좋습니다 것입니다.

for(i=0;j<10;i++) 
     if(white[i] == ' ') 
      to[0] = white[i-2]; 
      to[1] = white[i-1]; 

이것은 들여 쓰기가하는 것을 수행하지 않습니다. 실제로 이것입니다.

for(i=0;j<10;i++) 
     if(white[i] == ' ') 
      to[0] = white[i-2]; 

    to[1] = white[i-1]; 

이것이 우리가 always use braces 인 이유입니다.

관련 문제