2016-09-25 65 views
2

我有一個名爲「ChessMoves.h」的頭文件和一個c文件調用ChessMoves與一堆不同的功能。需要幫助從頭文件調用函數

頭文件

#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; 

文件我打電話給

#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]); 
} 

我們得到了一個文件來測試代碼,並在該文件中,他有:

whiteMove.color != WHITE 

和如果whiteMove.color不等於白色它將顯示「失敗」,所以我試圖 設置

whiteMove.color = WHITE 

但我不斷收到「請求會員'顏色'的東西不是結構或聯盟。我嘗試打電話的其他結構也是一樣。我試過了,

Move.color = WHITE 

而且這也不起作用。

+0

你爲什麼要測試未初始化的'white [0]'? – Sergio

+0

考慮到缺少分號,語法都不是C。 – bmargulies

+0

whiteMove.color = WHITE;應該是whiteMove-> color;這是一個指針。 – bmargulies

回答

2

所以我們可以編譯一些東西,我把這些全部放到一個文件中,剔除不相關的位,並添加了缺失的Color enum。

$ 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結構。因此它必須與->取消。 .用於直接訪問。

鐺的錯誤信息非常好,甚至給你一個建議如何解決它。我強烈建議你使用它,或者用同樣良好的錯誤的編譯器,同時學習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