2016-09-25 7 views
2

私は "ChessMoves.h"というヘッダファイルと、さまざまな機能を持つ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がWHITEと等しくない場合、「FAIL」と表示されますので、試しました。

whiteMove.color = WHITE 

しかし、私は構造体や共用体ではなく「メンバーの色の要求」を受けています。私が呼び出す他の構造体についても同じことが起こります。私はそれを試してみた

Move.color = WHITE 

そしてどちらも動作しません。

+0

なぜ初期化されていない 'white [0]'をテストしていますか? – Sergio

+0

セミコロンがないため、Cの構文はありません。 – bmargulies

+0

whiteMove.color = WHITE;は、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である理由です。

関連する問題