C语法错误:缺少'';在'之前;类型'; 我在微软Visual C++ 2010 Express中的C项目中得到了一个非常奇怪的语法错误。我有以下代码: void LoadValues(char *s, Matrix *m){ m->columns = numColumns(s); m->rows = numRows(s); m->data = (double*)malloc(sizeof(double) * (m->rows * m->columns)); int counter = 0; double temp; bool decimal; int numDec; while(*s != '\0'){ . . . } }

C语法错误:缺少'';在'之前;类型'; 我在微软Visual C++ 2010 Express中的C项目中得到了一个非常奇怪的语法错误。我有以下代码: void LoadValues(char *s, Matrix *m){ m->columns = numColumns(s); m->rows = numRows(s); m->data = (double*)malloc(sizeof(double) * (m->rows * m->columns)); int counter = 0; double temp; bool decimal; int numDec; while(*s != '\0'){ . . . } },c,syntax-error,visual-c++-2010,C,Syntax Error,Visual C++ 2010,当我尝试构建解决方案时,我的所有变量(temp、counter等)都会出现“missing”;“before type”错误,并且尝试在while循环中使用它们中的任何一个都会导致“undeclared identifier”错误。我确定布尔的定义是这样的 #ifndef bool #define bool char #define false ((bool)0) #define true ((bool)1) #endif 在.c文件的顶部。我在Stack Overfl

当我尝试构建解决方案时,我的所有变量(temp、counter等)都会出现“missing”;“before type”错误,并且尝试在while循环中使用它们中的任何一个都会导致“undeclared identifier”错误。我确定布尔的定义是这样的

#ifndef bool
    #define bool char
    #define false ((bool)0)
    #define true ((bool)1)
#endif
在.c文件的顶部。我在Stack Overflow中搜索了答案,有人说旧的C编译器不允许在同一块中声明和初始化变量,但我认为这不是问题所在,因为当我注释掉这些行时

m->columns = numColumns(s);
m->rows = numRows(s);
m->data = (double*)malloc(sizeof(double) * (m->rows * m->columns));
所有的语法错误都消失了,我不知道为什么。感谢您的帮助

---编辑---- 已请求矩阵的代码

typedef struct {
    int rows;
    int columns;
    double *data;
}Matrix;

<>在不符合C99(即微软Visual C++ 2010)的C编译器中,不能在块中间声明变量。< /P> 因此,请尝试将变量声明移动到块的顶部:

void LoadValues(char *s, Matrix *m){
    int counter = 0;
    double temp;
    bool decimal;
    int numDec;
    m->columns = numColumns(s);
    m->rows = numRows(s);
    m->data = (double*)malloc(sizeof(double) * (m->rows * m->columns));
    while(*s != '\0'){
        .
        .
        .
    }
}

您是否尝试将您尝试注释的三行移到变量定义下方,例如
int numDec之后?你的
类矩阵{…}定义后面跟一个分号,对吗?您需要显示更多代码。Matrix是什么?你是把它编译成C还是C++?MSC倾向于默认为C++,甚至.c文件添加了我的矩阵结构的代码,我相当自信它被编译成C,因为当我构建解决方案时,我得到警告,比如“C4244:=”警告:从“双”到“int”的转换,数据可能丢失,“C4244”部分使我想到C.Dukkel-感谢,这是完美的工作!是的,当我读到那篇文章时,我认为你不能做“int counter=0”;在一行中,你需要像“int counter;counter=0;”一样分解它。再次感谢!