c++ - '->' token 之前预期有不合格的 id,如何解决此问题?

标签 c++ c arrays pointers struct

struct box
{
    char word[200][200];
    char meaning[200][200];
    int count;
};

struct root {
    box *alphabets[26];
};
struct root *stem;
struct box *access;
void init(){
    int sizeofBox =  sizeof(struct box);
    for(int i = 0 ; i<= 25; i++){
        struct box *temp =(struct box*)( malloc(sizeofBox));
        temp->count = 0;
        root->alphabets[i] = temp; //error line
    }
}

Error: Expected unqualified-id before '->' token

如何修复这个错误。 谁能解释一下这是什么...??

最佳答案

root->alphabets[i] = temp;

这里root是一种类型。不允许在类型上调用 -> 。要使用此运算符,您必须有一个指向实例的指针。

我认为这一行应该是:

   stem->alphabets[i] = temp;
// ^^^^

但是这里会出现错误,因为没有为其分配内存。

所以这一行:

struct root *stem;

应该变成

root *stem = /* ... */; // keyword "struct" is not need here in c++

关于c++ - '->' token 之前预期有不合格的 id,如何解决此问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799680/

相关文章:

c++ - 如何在类似于 C 的 C++ 中打印 float

c++ - ACE C++ 登录多个文件

c++ - Char 到 int8_t 的转换给出了意想不到的结果?

c++ - 将 N x N 矩阵逆时针旋转 90 度。为什么填写值的顺序很重要?

c - Pthread Posix 质因数分解得到奇怪的结果?

c++ - 向用户询问 2 个数字时发生无限循环

c - 使用 pcap 段错误进行数据包嗅探

C - 顶层和底层进程之间的通信

c - qsort 比较全零

arrays - unshare() 和 copy() 有什么区别?