需要帮助从头文件调用函数

时间:2016-09-25 15:36:53

标签: c

我有一个名为“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不等于WHITE,它将显示“FAIL”,所以我尝试了 设置

whiteMove.color = WHITE

但是我不断得到“请求成员'颜色'的东西,而不是结构或联合。对于我试图调用的其他结构,同样的事情也是如此。我尝试制作它,

Move.color = WHITE

这也不起作用。

1 个答案:

答案 0 :(得分: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

的原因