在’ – >’标记之前预期的unqualified-id,如何解决这个问题?

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 ; icount = 0; root->alphabets[i] = temp; //error line } } 

错误:在’ – >’标记之前预期的unqualified-id

如何解决这个bug。 谁能解释一下这是什么类型的?

 root->alphabets[i] = temp; 

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

我认为这一行应该是:

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

但是你会在这里遇到错误,因为没有为它分配内存。

所以这一行:

 struct root *stem; 

应该成为

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

root是一种类型。 你不能在一个类型上调用operator -> 。 您需要一个指向实例的指针(或一个重载类型的实例-> )。 你不需要在c ++中的所有地方编写struct

 root* smth = ....; // look, no "struct" smth->alphabets[0] = ....; 

请注意,在C ++代码中广泛使用原始指针并不是惯用的。 修复此问题后,您将遇到其他问题。