gpt4 book ai didi

c++ - Elem 没有命名类型?

转载 作者:太空宇宙 更新时间:2023-11-04 12:10:52 27 4
gpt4 key购买 nike

我有一个相当简单的问题,我通常可以自己调试,但我现在似乎遇到了很多问题。

我正在创建一个链表数据结构,我做了两个函数,一个返回前面的 Elem,一个返回最后一个 Elem。问题是编译器说 Elem 没有定义类型,而实际上它定义了类型。

这里是相关代码的头文件:

class simpleList {

public:
//Returns a pointer to the first Elem of the list
simpleList::Elem* front();

//Returns a pointer to the last Elem of the list
simpleList::Elem* back();

private:
struct Elem {
char info;
Elem *next;
};

Elem *head;
};

下面是这两个函数的 .cpp 文件实现:

//Returns a pointer to the first Elem of the list
simpleList::Elem* simpleList::front()
{
return head;
}

//Returns a pointer to the last Elem of the list
simpleList::Elem* simpleList::back()
{
Elem * temp = head;

while( temp -> next != 0 )
temp = temp -> next;

return temp;
}

我已经尝试将它们的范围限定在类里面并只做:

Elem* simpleList::front()
Elem* simpleList::back()

错误信息如下: simpleList.h:38:9: 错误:‘class simpleList’ 中的‘Elem’ 没有命名类型 simpleList.h:41:9: 错误:‘class simpleList’ 中的‘Elem’ 没有命名类型

最佳答案

试试这个类声明的顺序:

class simpleList {
public:

        struct Elem {
            char info;
            Elem *next;
        };

        //Returns a pointer to the first Elem of the list
        Elem* front();

        //Returns a pointer to the last Elem of the list
        Elem* back();

    private:
        Elem *head;

};

关于c++ - Elem 没有命名类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9895126/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com