error: expected ';', ',' or ')' before '&' token

假如想象 提交于 2020-12-12 01:26:32

问题


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

In the line void cargarProducto (Producto &p) throws the following error: 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;
    }
}

The same in the line void copiar (Producto &destino, Producto origen)


回答1:


If your intention is to have copiar directly modify destino, you must pass the address of destino. Passing by reference doesn't exist in 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;
}

It's better to pass structures by address, even if you don't plan on modifying their contents. This is because structures may be large and you don't want to put them on the stack if not needed. Declare structures const where needed. In the above code, the address of destino is constant, but not the data; for origen, both the address and data are constant.




回答2:


You're apparently writing a C++ program, not a C program. Use the correct compiler in this case (g++) and one of correct extensions like .cc, .cpp, .cxx...).




回答3:


The code presented in the question did not work since it is a "mix" of C with C ++. Here I present the correct code implemented in 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;
}


来源:https://stackoverflow.com/questions/20831221/error-expected-or-before-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!