huaijuliu 发表于 2011-6-19 04:44:44

请教关于简单输出显示的问题

新手向各位前辈问好。有一个很小的程序,编译有一个错误: error C2819: type 'Stack' does not have an overloaded member 'operator ->' 希望朋友们能帮忙一下。感谢。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


/* FMG Solver of the EHL circular contact */
#define pi 3.1415926535897931

typedef struct
{
double hx;
int ii;
double *p, *f;
double *hfi, *hrhs;
double *w;
double *K;
double *pjac;/*old pressure for use in Jacobi relaxation*/
double rg;
double Hm;
} Level;


typedef struct
{
int nx0;
int maxlevel;
double xa,xb;
double h0;
Level *Lk;
} Stack;

void initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0)
{
/* initialize values in datastructure */
double hx;
Level *L;
int l,ii;
U->xa=xa;
U->xb=xb;
U->maxlevel=maxl;
U->h0=h0;
U->Lk=(Level *)calloc(maxl+1,sizeof(Level));
hx=(xb-xa)/nx0;
ii=nx0;
for (l=1; l<=maxl; l++)
{
L=U->Lk+l;
L->hx=hx;
L->ii=ii;
L->p =(double *)calloc(U->maxlevel+1,sizeof(double));
L->w =(double *)calloc(U->maxlevel+1,sizeof(double));
L->f =(double *)calloc(U->maxlevel+1,sizeof(double));
L->pjac =(double *)calloc(U->maxlevel+1,sizeof(double));
L->hfi =(double *)calloc(U->maxlevel+1,sizeof(double));
L->hrhs=(double *)calloc(U->maxlevel+1,sizeof(double));
L->K =(double *)calloc(U->maxlevel+1,sizeof(double));
printf("\n level: %2d ii=%4d, hx=%f",l,ii,hx);
hx*=0.5; ii*=2;
}
}

void main()
{
Stack U;
initialize(&U,128,1,-4.5,1.5,1);
printf("xa in U is %f",U->xa);
system("pause");
}

huaijuliu 发表于 2011-6-19 04:46:24

本帖最后由 huaijuliu 于 2011-6-19 05:24 编辑

这个问题我已经找到了把printf那句改为
printf("\n xa in U is %f",U.xa);即可
能否请大家帮忙解释一下为什么main函数中定义Stack的时候为Stack U而其它函数如initialize中的参数为initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0)带星号呢?

caoer 发表于 2011-6-19 10:36:19

指针啊,大哥,c基础
Stack U; 中的U是个object
Stack *U 中的U是个pointer,
写这个程序的人也够呛,故意为难新手吧,如果是我就写成
Stack *pU
页: [1]
查看完整版本: 请教关于简单输出显示的问题