' ='之前的预期符号代币

时间:2014-07-19 08:08:53

标签: c syntax-error

我正在使用C进行tic tac toe游戏,我的代码存在一些问题。

player.h:

#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED

#include "gameLib.h"

struct Player
{
    int score = 0;
    char name[15];
    char symbol;
    int status;
};
void incraseScore(struct* Player p);
void changeName(struct* Player p, char newName[]);
void changeStatus(struct* Player p, int status);
void writeSymbol(struct* Player p, int locaton);

#endif // PLAYER_H_INCLUDED

player.c:

#include <player.h>

void incraseScore(struct* Player p)
{
    p->score += 1;
}

void changeName(struct* Player p, char newName[])
{
    p->name[] = newName[];
}

void changeStatus(struct* Player p, int status)
{
    p->status = status;
}

void writeSymbol(struct* Player p, int locaton)
{
    int i, j;
    extern char table[3][3];

    for (i=0; i<3; ++i){
        for (j=0; j<3; ++j){
            if (table[i][j] != 'X' && table[i][j] != 'O'){
                if (table[i][j] == 3*i+j+1){
                    table[i][j] = p->symbol;
                    break;
                }
            }
        }
    }
}

gameLib.h:

#ifndef GAMELIB_H_INCLUDED
#define GAMELIB_H_INCLUDED

char table[3][3];

#endif // GAMELIB_H_INCLUDED

main.h:

#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED

#include "player.h"
#include "gameLib.h"

#endif // MAIN_H_INCLUDED

main.c中:

#include <stdio.h>
#include <stdlib.h>
#include "main.h"

int main()
{
    printf("Hello world!\n");
    return 0;
}

在player.h中我收到5个错误:

on line 8: expected ':', ',', ';', '}' or '__attribute__' before '=' token
and on lines 13, 14, 15, 16: expected '{' before '*'

我不明白为什么我收到这个。我是结构和标题的初学者,所以慢慢来。谁能解释我的错误并纠正我?

谢谢!

1 个答案:

答案 0 :(得分:3)

有两个问题,

首先,虽然非静态数据成员的初始化是合法的C ++,但在C:

中是不允许的
struct Player
{
    int score = 0; // ERROR in C
    ....
};

其次,这在语法上是无效的:

void incraseScore(struct* Player p);
//                      ^ this has no place here

你需要

void incraseScore(struct Player* p);