简体   繁体   English

错误:预期在'='标记之前的':',',',',',','}'或'__attribute__',错误:预期在'va'之前的预期')'

[英]error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token & error: expected ‘)’ before ‘va’

I am compiling the below c and .h codes in GCC terminal (centos linux) for an ATM project received the below errors. 我正在GCC终端(centos linux)的ACC项目中编译以下c和.h代码,并收到以下错误。 Please help as I am new to programming. 请帮助,因为我是编程新手。

validate_acc.h
#ifndef _VALIDATE_ACC_
#define _VALIDATE_ACC_

struct validate_acc {
int user_acc_try, i = 0;
 int user_has_not_entered_right_acc = 1;
 int retries = 3;
  };

 typedef struct validate_acc Validate_acc;




  #endif

=============================================== ==========================================

validate_acc.c

#include<stdio.h>
 #include "validate_acc.h"

  extern int account_number;

  void (Validate_acc va)
{


va.user_acc_try, va.i = 0;
va.user_has_not_entered_right_acc = 1;
va.retries = 3;



 while(va.retries > 0 && va.user_has_not_entered_right_acc == 1){
               printf("\nPlease enter your account number: ");
               scanf("%d", &va.user_acc_try);

               if(va.user_acc_try != account_number){
                                   printf("You entered the wrong     account      number\n");
                               va.retries--;
                               }
               else{
               va.user_has_not_entered_right_acc = 0;
               }
               }
 }

====================errors ===================错误

[linux@localhost Assignment1]$ gcc -c validate_acc.c
In file included from validate_acc.c:2:0:
validate_acc.h:5:23: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or   ‘__attribute__’ before ‘=’ token
  int user_acc_try, i = 0;
                   ^
validate_acc.c:6:20: error: expected ‘)’ before ‘va’
 void (Validate_acc va)

Structure type declarations do not support initializers, such as you are trying to provide. 结构类型声明不支持初始化程序,例如您要提供的初始化程序。 Moreover, intializers for structure objects are expressed quite differently than you are trying to do. 此外,结构对象的初始化符的表达方式与您尝试执行的表达方式完全不同。 Your struct declaration should have this form: 您的struct声明应采用以下形式:

struct validate_acc {
    int user_acc_try;
    int i;
    int user_has_not_entered_right_acc;
    int retries;
};

That declares a type , not (in itself) any object with associated storage that you could initialize. 那声明了一个type ,而不是(本身)声明了可以初始化的具有关联存储的任何对象。 That's why you can declare a type alias for it as you have done: 这就是为什么您可以为它声明类型别名的原因:

typedef struct validate_acc Validate_acc;

If you want to perform initialization of objects of that type then you must do it on a per-object basis, like so: 如果要初始化该类型的对象,则必须基于每个对象进行初始化,如下所示:

struct validate_acc validate_object = { 0, 0, 1, 3 };

or, using the typedef ed alias: 或者,使用typedef ed别名:

validate_acc validate_object = { 0, 0, 1, 3 };

Perhaps your idea was to provide default values for your structure's members, but C does not have such a feature. 也许您的想法是为结构的成员提供默认值,但是C没有这种功能。 You can create a shortcut initialization macro, however, such as 您可以创建快捷方式初始化宏,例如

#define VALIDATE_ACC_DEFAULTS { 0, 0, 1, 3 }

. You could then do 然后你可以做

struct validate_acc validate_object = VALIDATE_ACC_DEFAULTS;

That doesn't shorten the code in this case, but it does make it clearer. 在这种情况下,这不会缩短代码,但可以使代码更清晰。 Supposing you put the macro definition in the same header is the structure declaration, it also provides a central place where you can change the default initialization values. 假设将宏定义放在同一标头中的是结构声明,它还提供了一个中心位置,您可以在其中更改默认的初始化值。

暂无
暂无

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

相关问题 错误:“ {”令牌|之前的预期&#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 :1:错误:在&#39;{&#39;标记之前应为&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - :1: 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 错误:在“*”标记之前应有“=”、“、”、“;”、“asm”或“__attribute__” - error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token 在“{”标记之前应使用“:”、“,”、“;”、“}”或“__attribute__” - expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘{’ token 结构堆栈错误:在“=”标记之前应为“:”、“、”、“;”、“}”或“__attribute__” - struct stack error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token 仍然是“错误:在&#39;*&#39;标记之前出现预期的&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39;” - Still the “error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM