C 错误:应为'''';或';)';在'之前&';代币
在void行中,cargarProducto(Producto&p)抛出以下错误:错误:应为“;”,“,”或“&”标记之前的“')C 错误:应为'''';或';)';在'之前&';代币,c,gcc,token,C,Gcc,Token,在void行中,cargarProducto(Producto&p)抛出以下错误:错误:应为“;”,“,”或“&”标记之前的“') void cargarProducto (Producto &p) { printf("\nIngrese el c¢diqo: "); scanf("%d", &p.codigo); fflush(stdin); printf("\nIngrese la descripci¢n: "); int i;
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);
}
}
<> P>在线空隙COPIAR(生产和Desto,产品OrrEnEn) < P>你显然是在写C++程序,而不是C程序。在这种情况下,请使用正确的编译器(
g++
)和正确的扩展名之一,如.cc
,.cpp
,.cxx
…) 如果您打算让copiar
直接修改destino
,则必须传递destino
的地址。按引用传递在C中不存在
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;
}
}
最好按地址传递结构,即使您不打算修改它们的内容。这是因为结构可能很大,如果不需要,您不想将它们放在堆栈上。在需要时声明结构
const
。在上述代码中,destino
的地址是常数,但数据不是常数;对于origen
,地址和数据都是常量。问题中的代码不起作用,因为它是C与C++的“混合体”。这里我展示了用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;
}
#包括
typedef枚举{导入,本地}类型;
//定义数据结构类型的产品结构
类型定义结构
{
int代码;
字符描述[20];
煤焦测定单位[5];
浮动价格;
类型判别器;
联盟
{
煤焦来源[20];
目的地字符[20];
国际电话;
}世博会;
}产品;
//程序装入产品
无效载荷产品(产品*p)
{
printf(“\n输入代码:”);
scanf(“%d”,&p->code);
fflush(stdin);
printf(“\n输入说明:”);
scanf(“%s”,p->description);
printf(“表示度量单位:”);
scanf(“%s”,p->MeasureUnit);
printf(“输入价格:”);
scanf(“%f”,&p->price);
int i;
printf(“\n如果导入,则插入1”);
scanf(“%d”、&i);
如果(i==1)
{
p->discriminant=导入;
}
其他的
{
p->判别式=局部;
}
if(p->discriminant==导入)
{
printf(“\n输入源:”);
获取(p->expo.origin);
}
其他的
{
printf(“\n输入电话”);
scanf(“%d”和&p->expo.telephone);
}
}
无效副本(产品*常量目的地,常量产品*常量来源)
{
目的地->代码=起点->代码;
(*目的地->说明)=(*起点->说明);
(*目的地->测量单位)=(*起点->测量单位);
目的地->价格=原产地->价格;
目的地->鉴别点=起点->鉴别点;
如果(目的地->判别==导入)
(*destination->impoExpo.origin)=(*origin->impoExpo.origin);
其他的
目的地->impoExpo.telephone=起点->impoExpo.telephone;
}
<代码>无效。也许你正在尝试编写一个C++程序?@ zeta好点。错过了&
。不相关,但您确定要用单个字符编码起始字符吗?世界上有比可打印ASCII字符更多的国家。这能回答你的问题吗?考虑到使用C标准I/O而不是C++,Alejandro是否知道他是在写C还是C++(不知道是标签表示C而不是C++),但是编译器是C编译器,而<代码>和<代码>,就像C++代码一样,所以,在代码变为C或者编译器被转换为C++之前,这两个程序都不会很快乐。@ JONAATEN LIFFLLER:是的,他的程序结构表明他需要普通C,但是从C++看到了引用语法,并认为它也可能是C。对于C变体,已经有一个很好的答案,而在C++中,如果成员/逻辑是/简单的话,不需要写一个复制函数(尽管如果//or对我来说目前还没有意义)。
#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;
}