C++ C错误:在'之前应为不合格id;[';代币
我想写一个程序,可以得到一个句子中每个不同字符的数量。但是当我使用gcc编译代码时,它会显示如下错误: 错误::在“[”标记之前应为非限定id。这些错误发生在以下行中:C++ C错误:在'之前应为不合格id;[';代币,c++,C++,我想写一个程序,可以得到一个句子中每个不同字符的数量。但是当我使用gcc编译代码时,它会显示如下错误: 错误::在“[”标记之前应为非限定id。这些错误发生在以下行中: CountMachine[cnt].ch=*(S.ch); CountMachine[cnt].count++; if(*(S.ch)==CountMachine[j].ch) ..... (where I use CountMachine[]). 这是我的完整代码: CountChar
CountMachine[cnt].ch=*(S.ch);
CountMachine[cnt].count++;
if(*(S.ch)==CountMachine[j].ch)
.....
(where I use CountMachine[]).
这是我的完整代码:
CountChar.h:
typedef struct
{
char ch;
int count=0;
}CountMachine[50];
typedef struct
{
char *ch;
int length;
}HString;
CountChar.cpp(但我使用C的语法)
void CountChar(hs字符串S)
{
int-cnt=0;
对于(int i=0;i您有一个非常奇特的类型别名,它声明了一个CountMachine
类型,而不是一个包含50个未命名结构数组的变量
typedef struct
{
char ch;
int count=0;
}CountMachine[50];
// CountMachine is a type (array 50 of unnamed struct)
// step-by step declaration is much more clear:
struct machine
{
char ch;
int count=0;
};
typedef struct machine machine_t;
machine_t machines[50];
// machines is a variable that holds an array of 50 machine_t
在CountChar.h
中将CountMachine
声明为包含50个结构的类型,其中包含一个字符和一个整数,然后在CountChar.cpp
中寻址该类型本身
您无法在类型中寻址特定项,您需要创建一个类型为CountMachine
的变量,或者从标题中的CountMachine
声明中删除关键字typedef
。CountMachine
是typedef的类型名称。@PaulOgilvie在C中有效,但它是int count=0;的一部分。
C.中无效,C++中没有“C语法”。C和C++是不同的语言。相同的语法可以有不同的语义(如所指出的,这是无效的C)。不要垃圾邮件标签。@奥拉夫,我的意思是我使用C语法,只是编译我的代码在C++环境下。这样做了吗?我做了很长时间:我在C中编程,保存它作为.CPP文件并使用GCC编译它。到目前为止,我所有的代码都正确运行(除了这一个)。你可以在老师面前引用我:他们在传播这些废话之前应该先学习语言。在1980年代(90年代早期,C++没有标准化,C是在K & R RES.ANSI-C(C89/90))可能是正确的。使用C99/C++98时,这已成为危险的建议,但自6年以来,所有教授这一点的人都应该立即被解雇。如果我看到我的项目成员中有人这样做,他会严肃地说要阻止这一点,否则就会被踢。值得注意的是,以\u t
结尾的类型保留给…POSIX使用,我想是的。
typedef struct
{
char ch;
int count=0;
}CountMachine[50];
// CountMachine is a type (array 50 of unnamed struct)
// step-by step declaration is much more clear:
struct machine
{
char ch;
int count=0;
};
typedef struct machine machine_t;
machine_t machines[50];
// machines is a variable that holds an array of 50 machine_t