简体   繁体   English

在“{”标记之前应使用“:”、“,”、“;”、“}”或“__attribute__”

[英]expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token

Can't find a mistake that causes the error in the title.找不到导致标题错误的错误。 Have been seeking for it for an hour, would be glad if someone helped me find the bug.已经找了一个小时了,如果有人帮我找到错误,我会很高兴。 I see no missing, and;我没有看到遗漏,并且; or what-so-ever:( There is really not much need to add comments about the code purpose, just an annoying syntax error. I'm pretty much new with C, therefore the mistake might be obvious, however I have absolutely no clue concerning this part of the code and why it refuses to compile with gcc, but works fine with compiler integrated into CLion. I thought that CLion basically used the gcc, but none the less it is not what really important to me. Hence I would be really thankful to the one, who could guide me through. Full error message: error: expected ':', ',', ';', '}' or ' attribute ' before '{' token 92 | void initialize(int _height, int _width) {或者什么都没有:(真的没有太多需要添加关于代码目的的注释,只是一个烦人的语法错误。我对 C 很陌生,因此错误可能很明显,但我完全不知道关于这部分代码以及为什么它拒绝使用 gcc 进行编译,但与集成到 CLion 中的编译器一起工作正常。我认为 CLion 基本上使用了 gcc,但无论如何它对我来说并不是真正重要的。因此我会非常感谢那个可以指导我完成的人。完整的错误消息:错误:在“{”令牌 92 | void 初始化(int _height , 整数_宽度) {

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

// Definicji objektów na mapie (1)
#define ROCKFORD '@'
#define EMPTY ' '
#define DIRT '+'
#define HILL '#'
#define ROCK 'O'
#define DIAMOND '$'
#define EXIT 'X'

// Definicji kierunków ruchu (2)
#define UP 'w'
#define LEFT 'a'
#define DOWN 's'
#define RIGHT 'd'
#define NOTHING '0'

#define NEW_LINE (char) 10

typedef char object; // objekt na mapie, możliwe wartości są w definicji nr (1)
typedef char move; // kierunek ruchu, możliwe wartości są w definicji nr (2)

// *** Funkcjonal związany z typem move ***

// Wczytanie instrukcji ruchów oraz zwracanie false przy nowej linii
char readMove(move* command) {
    *command = (char) getchar();
    return (*command != NEW_LINE && *command != EOF);
}

// Wczytanie jednego symbolu w pustość aby pozbyć '\n'
void skipNewline() {
    getchar();
}

// Sprawdzenie, czy wczytany symbol nie jest końcem wejścia
bool isValid(move command) {
    return (command != EOF);
}

// Przetwarzanie kierunku ruchu na zmianę poziomowej współrzędnej
int verticalShift(move mv) {
    if (mv == UP || mv == DOWN)
        return (mv == UP ? -1 : 1);
    return 0;
}

// Przetwarzanie kierunku ruchu na zmianę pionowej współrzędnej
int horizontalShift(move mv) {
    if (mv == LEFT || mv == RIGHT)
        return (mv == LEFT ? -1 : 1);
    return 0;
}

// Struktura, reprezentująca współrzędną
typedef struct point {
    int x, y;
} point;

// *** Funkcjonal związany ze strukturą point ***

// Stworzenie współrzędnej
point makePoint(int x, int y) {
    point p;
    p.x = x, p.y = y;
    return p;
}

// Dodawanie odpowiednich współrzęd do siebie
point addPoints(point p1, point p2) {
    return makePoint(p1.x + p2.x, p1.y + p2.y);
}

// Sprawdzenie równości pomiędzy dwoma współrzędnymi
bool samePoints(point p1, point p2) {
    return (p1.x == p2.x && p1.y == p2.y);
}

// Typ danych reprezentujący planszę
typedef struct board {
    int height; // wysokość planszy
    int width; // szerokość plansy
    int diamonds; // liczba diamentów na planszy
    point rf; // pozycja Rockfordu w tablicy field
    object** field; // mapa objektów planszy
    bool reachedExit; // true wtw, gdy Rockford opuścił planszę

    // Inicjalizacja planszy
    void initialize(int _height, int _width) {
        height = _height;
        width = _width;
        field = (object **) malloc(sizeof(object *) * (size_t) height);
        for (int i = 0; i < height; i++)
            field[i] = (object *) malloc(sizeof(object) * (size_t) width);
        diamonds = 0;
        reachedExit = false;
    }

    // Otrzymanie objektu na planszy o współrzędnej pos
    object get(point pos) {
        return field[pos.y][pos.x];
    }

    // Zamiana objektu na planszy o współrzędnej pos na val
    void set(point pos, object val) {
        field[pos.y][pos.x] = val;
    }

    // Pokazywanie objektu na planszy o współrzędnej pos
    void show(point pos) {
        putchar(samePoints(rf, pos) && !reachedExit ? ROCKFORD : get(pos));
    }

    // Stabilizowanie planszy
    void stabilize() {
        point pos;
        for (pos.y = height - 1; pos.y >= 0; pos.y--) {
            for (pos.x = width - 1; pos.x >= 0; pos.x--) {
                object cur = get(pos);
                if (cur == DIAMOND || cur == ROCK) {
                    int dy = 0;
                    while (this->get(pos) == EMPTY && !samePoints(rf, pos)) dy++;
                    set(pos, EMPTY);
                    set(makePoint(pos.y + dy, pos.x), cur);
                }
            }
        }
    }

    // Pokazywanie każdego objektu na planszy
    void showBoard() {
        printf("%d %d\n", height, width);
        point pos;
        for (pos.y = 0; pos.y < height; pos.y++) {
            for (pos.x = 0; pos.x < width; pos.x++)
                show(pos);
            printf("\n");
        }
    }

    // Oczyszczenie pamięci alokowanej dla planszy
    void clear() {
        for (int i = 0; i < height; i++)
            free(field[i]);
        free(field);
    }
} board;

The way to do OOP in C (at least one of them) is to:在 C(至少其中之一)中执行 OOP 的方法是:

  1. Have structs as classes将结构作为类
  2. Declare member functions (as normal functions) outside the struct在结构外声明成员函数(作为普通函数)
  3. Member functions should have a prefix that identifies what class it belongs to成员函数应该有一个前缀来标识它属于什么 class
  4. Member functions should take as first parameter the object they are meant to be applied to.成员函数应该将它们要应用到的 object 作为第一个参数。

Points 4,5,6 etc deals with multiple inheritance and virtual methods, I am not going to go into this now.第 4、5、6 点等涉及多个 inheritance 和虚拟方法,我现在不打算将 go 纳入其中。

So as example:举个例子:

struct board {
    int height;
    int width;
    int diamonds;
    point rf;
    /*
      This is not a good way to declare a 2d array, but fixing
      that is outside the scope of this answer
     */  
    object** field;
    bool reachedExit;
};

// Inicjalizacja planszy
void board_initialize(struct board *b, int height, int width) {
    b->height = height;
    b->width = width;

    /* TODO: Malloc needs error checking */
    b->field = malloc(sizeof(*b->field) * (size_t) height);
    for (int i = 0; i < height; i++)
        b->field[i] = malloc(sizeof(*b->field[i]) * (size_t) width);
    b->diamonds = 0;
    b->reachedExit = false;
}

/*
  `*b` is `const` since we don't intend to modify whatever `b` 
  points at
*/  
object board_get(const struct board *b, point pos) {
    return b->field[pos.y][pos.x];
}

void board_clear(struct board *b) {
    for (int i = 0; i < b->height; i++)
        free(b->field[i]);
    free(b->field);
}

Rest of methods are left out as exercise to the reader. Rest 种方法作为练习留给了读者。

Constructors can be implemented as either returning a struct as value, or returning a pointer to malloc -ed memory:构造函数可以实现为返回一个struct作为值,或者返回一个指向malloc -ed memory 的指针:

struct board board_create(int h, int w)
{
   struct board b;
   board_initialize(&b, h, w);
   return b;
}

struct board *board_alloc(int h, int w)
{
   struct board *b = malloc(sizeof(*b));
   if (b)
      board_initialize(b, h, w);
   return b;
}

/* `board_malloc` also needs `board_free` */
void board_free(struct board *b)
{
   board_clear(b);
   free(b);
}

Example on how to use this:有关如何使用它的示例:

struct board b1 = board_create(w, h);
object o1 = board_get(&b1, pos);
board_clear(&b1);

/*****************************/

struct board *b2 = board_alloc(w, h);
if (!b2)
  {
     perror("Allocation failed");
     exit(EXIT_FAILURE);
  }
object o2 = board_get(b2, pos);
board_free(b2);

Personally I prefer board_create / board_clear to board_malloc / board_free .我个人更喜欢board_create / board_clearboard_malloc / board_free I am of the opinion that the owner of a struct board should decide how and where its memory is allocated and stored.我认为struct board的所有者应该决定其 memory 的分配和存储方式和位置。 But this is a matter of style.但这是风格问题。

Naturally you could also bite the bullet and learn c++. In that case you should get rid of malloc / free and use container classes.当然你也可以硬着头皮学习 c++ 。在这种情况下你应该摆脱malloc / free并使用容器类。 Or use smart pointers in those rare cases where there is no fitting container class.或者在没有合适容器 class 的极少数情况下使用智能指针。

Total disclaimer: Any bugs in original code are left as is, and there may be some additional new, minor ones.完全免责声明:原始代码中的任何错误均保持原样,并且可能会有一些新的、次要的错误。 Free work doesn't come with warranties.免费工作不附带保证。

I don't understand why you have functions declared inside a struct.我不明白为什么要在结构中声明函数。 You should define the struct and then declare your functions你应该定义结构然后声明你的函数

typedef struct board {
    int height; // wysokość planszy
    int width; // szerokość plansy
    int diamonds; // liczba diamentów na planszy
    point rf; // pozycja Rockfordu w tablicy field
    object** field; // mapa objektów planszy
    bool reachedExit; // true wtw, gdy Rockford opuścił planszę
} board;

/* void initialize */

/* void get  */

...

Also, I would personally put the struct at the start, just after the various define .另外,我个人会把 struct 放在开头,就在各种define之后。 It doesn't change anything tho.它不会改变任何东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 :1:错误:在&#39;{&#39;标记之前应为&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - :1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 在&#39;&#39;之前预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39;。 代币 - expected '=', ',', ';', 'asm' or '__attribute__' before '.' token “在&#39;-&gt;&#39;标记之前的预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - "Expected '=', ',', ';', 'asm' or '__attribute__' before '->' token 错误:在 '.' 之前应有 '='、','、';'、'asm' 或 '__attribute__' 令牌 - error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token 错误:“ {”令牌|之前的预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token| 错误:在&#39;:&#39;标记前应有&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token 错误:在'{'令牌之前预期'=',',',';','asm'或'__attribute__' - error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 错误:结构中&#39;=&#39;标记之前的预期&#39;:&#39;,&#39;,&#39;,&#39;;&#39;,&#39;}&#39;或&#39;__attribute__&#39; - error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token in struct 在&#39;{&#39;令牌之前预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 错误:在“*”标记之前应有“=”、“、”、“;”、“asm”或“__attribute__” - error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM