繁体   English   中英

错误:在'&'标记之前预期';',','或')'

[英]error: expected ';', ',' or ')' before '&' token

    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);
    }
}

在行中,cargarProducto(Producto&p)抛出以下错误:错误:在'&'标记之前预期';',','或')'

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;
    }
}

行无效copiar(Producto&destino,Producto origen)

如果您的目的是让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 ,地址和数据都是常量。

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

问题中提供的代码不起作用,因为它是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;
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM