求教:va_list的使用
以下代码在编译时出错,错误信息是 "C2338 va_start argument must not have reference type and must not be parenthesized"。好像是说参数不能是引用。但是InitArray()调用编译是成功的,就是Value和Assign有问题。有其他办法解决吗?谢谢!
typedef struct
{
int id;
char firstName[35];
} ElemType;
typedef struct
{
ElemType data;
int dim;
int *bounds;
int *constants;
}
int Locate(Array A, va_list ap, int &offset)
{
int ind;
offset = 0;
for (int i = 0; i < A.dim; ++i)
{
ind = va_arg(ap, int);
if (ind < 0 || ind >= A.bounds[i])
return OVERFLOW;
offset += A.constants[i] * ind;
}
return OK;
}
Status Value(Array A, ElemType &e, ...)
{
int offset;
va_list ap;
va_start(ap, e);
int result = Locate(A, ap, offset);
va_end(ap);
if (result != OK)
return result;
e = *(A.base + off);
return OK;
}
Status Assign(Array &A, ElemType e, ...)
{
int offset;
va_list ap;
int result = Locate(A, ap, offset);
if (result != OK)
return result;
*(A.base + off) = e;
return OK;
}
InitArray(Array &A, int dim, ...)
{
if (dim<1 || dim>MAX_ARRAY_DIM)
return ERROR;
A.dim = dim;
A.bounds = (int*)malloc(dim * sizeof(int));
if (!A.bounds)
exit(OVERFLOW);
// Calculate elem total
int elemTotal = 1;
va_list ap;
va_start(ap, dim);
for (int i = 0; i < dim; ++i)
{
A.bounds[i] = va_arg(ap, int);
if (A.bounds < 0)
return (UNDERFLOW);
elemTotal *= A.bounds[i];
}
va_end(ap);
A.base = (ElemType*)malloc(elemTotal * sizeof(ElemType));
if (!A.base)
exit(OVERFLOW);
A.constants[dim - 1] = 1; // L=1; point increase or decrease by size of ElemType
for (int i = dim - 2; i >= 0; --i)
{
A.constants[i] = A.constants[i + 1] * A.bounds[i + 1];
}
return OK;
}