【问题标题】:expected unqualified-id before '->' token, How to fix this?'->' 标记之前的预期 unqualified-id,如何解决这个问题?
【发布时间】:2013-09-14 08:06:57
【问题描述】:
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
    }
}

错误:在 '->' 标记之前需要不合格的 id

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

【问题讨论】:

  • 您可能指的是stem 而不是root,除了stem 永远不会设置为任何值,这样会在运行时崩溃。也许将struct root* stem; 更改为struct root stem;(无指针)。

标签: c++ c arrays pointers struct


【解决方案1】:
root->alphabets[i] = temp;

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

我认为这行应该是:

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

但是这里会报错,因为没有为它分配内存。

所以这一行:

struct root *stem;

应该变成

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

【讨论】:

    【解决方案2】:

    root 是一种类型。您不能在类型上调用运算符-&gt;。您需要一个指向实例(或重载-&gt; 的类型的实例)的指针。你也不需要在 C++ 中到处写struct

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

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

    【讨论】:

    • 但我也使用了那个 '*stem'。错误:“->”标记之前的预期主表达式
    • @KarthikSivam 你能否提供一些重现问题的最小代码(并且不包括其他令人分心的错误?)
    猜你喜欢
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多