【问题标题】:error: expected ';', ',' or ')' before '&' token错误:在 '&' 标记之前应为 ';'、',' 或 ')'
【发布时间】:2014-01-16 20:20:35
【问题描述】:
    void cargarProducto (Producto &p)
{
    printf("\nIngrese el c¢diqo: ");
    scanf("%d", &p.codigo);
    fflush(stdin);
    printf("\nIngrese la descripci¢n: ");
    int i;
    printf("\nIngrese 1 si es importado");
    scanf("%d", &i);
    if(i == 1)
    {
        p.discriminante = IMPORTADO;
    }
    else
    {
        p.discriminante = LOCAL;
    }
    if(p.discriminante == IMPORTADO)
    {
        printf ("\nIngrese al origen:");
        scanf("%c", &p.origen);
    }
    else
    {
        printf ("\nIngrese el telefono");
        scanf ("%d", &p.impoExpo.telefono);
    }
}

在 void cargarProducto (Producto &p) 行中抛出以下错误:error: expected ';', ',' or ')' before '&' token

void copiar (Producto &destino, Producto origen)
{
    destino.codigo=origen.codigo;
    destino.descripcion=origen.descripcion;
    destino.unidadMedida=origen.unidadMedida;
    destino.precio=origen.precio;
    destino.discriminante.origen.discriminante;
    if (destino.discriminante ==IMPORTADO)
    {
        destino.impoExpo.origen=origen.impoExpo.origen;
    }
    else
    {
        impoExpo.telefono=origen.impoExpo.telefono;
    }
}

同在线void copiar(Producto &destino, Producto origen)

【问题讨论】:

  • 这不是有效的 C。也许您正在尝试编写 C++ 程序?
  • @Zeta 好点。错过了&
  • 不相关,但您确定要使用单个字符对原点进行编码吗?世界上的国家比可打印的 ASCII 字符多。

标签: c gcc token


【解决方案1】:

您显然是在编写 C++ 程序,而不是 C 程序。在这种情况下使用正确的编译器 (g++) 和正确的扩展之一,如 .cc.cpp.cxx...)。

【讨论】:

  • 鉴于使用 C 标准 I/O 而不是 C++,尚不清楚 Alejandro 是否知道他是在编写 C 还是 C++(尽管标签建议使用 C 而不是 C++)——但编译器是一个 C 编译器和像那样使用的 & 是 C++ 代码,所以在将代码更改为 C 或编译器更改为 C++ 之前,两者都不会高兴。
  • @JonathanLeffler:是的,他的程序结构表明他想要纯 C,但看到 C++ 的参考语法并认为它也可能是 C。对于 C 变体,已经有一个很好的答案,而在 C++ 中,如果成员/逻辑如此简单,则不需要编写复制函数(尽管目前 if/else 对我没有意义)。
【解决方案2】:

如果你的意图是让copiar直接修改destino,你必须传递destino的地址。 C 中不存在通过引用传递。

void copiar (Producto * const destino, const Producto * const origen)
{
    destino->codigo = origen->codigo;
    destino->descripcion = origen->descripcion;
    destino->unidadMedida = origen->unidadMedida;
    destino->precio = origen->precio;
    destino->discriminante = origen->discriminante;

    if(destino->discriminante == IMPORTADO)
        destino->impoExpo.origen = origen->impoExpo.origen;
    else
        impoExpo->telefono = origen->impoExpo.telefono;
}

最好按地址传递结构,即使您不打算修改其内容。这是因为结构可能很大,如果不需要,您不想将它们放在堆栈上。在需要的地方声明结构const。上面代码中destino的地址是常量,但不是数据;对于origen,地址和数据都是常量。

【讨论】:

    【解决方案3】:

    问题中提供的代码不起作用,因为它是 C 与 C++ 的“混合”。在这里,我展示了用 C 实现的正确代码。

        #include <stdio.h>
    
        typedef enum {IMPORTED, LOCAL} type;
    
        //Product structure that defines the type of data structure
        typedef struct
        {
            int code;
            char description [20];
            char MeasureUnit [5];
            float price;
            type discriminant;
            union
            {
                char origin [20];
                char destination [20];
                int telephone;
            } impoExpo;
        } Product;
    
    //procedure loadProduct
    void loadProduct (Product *p)
    {
        printf("\nEnter the code:");
        scanf("%d",&p->code);
        fflush(stdin);
        printf("\nEnter the description:");
        scanf("%s",p->description);
        printf("Indicate the unit of measure:");
        scanf("%s",p->MeasureUnit);
        printf("Enter the price:");
        scanf("%f",&p->price);
        int i;
        printf("\nInsert 1 if imported");
        scanf("%d", &i);
        if(i == 1)
        {
            p->discriminant = IMPORTED;
        }
        else
        {
            p->discriminant = LOCAL;
        }
        if(p->discriminant == IMPORTED)
        {
            printf("\nEnter source: ");
            gets(p->impoExpo.origin);
        }
        else
        {
            printf("\nEnter the phone");
            scanf("%d", &p->impoExpo.telephone);
        }
    }
    
    void copy (Product * const destination, const Product * const origin)
    {
        destination->code = origin->code;
        (*destination->description) = (*origin->description);
        (*destination->MeasureUnit) = (*origin->MeasureUnit);
        destination->price = origin->price;
        destination->discriminant = origin->discriminant;
    
        if(destination->discriminant == IMPORTED)
            (*destination->impoExpo.origin) = (*origin->impoExpo.origin);
        else
            destination->impoExpo.telephone = origin->impoExpo.telephone;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-05
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多