C++类没有名为“blah”的成员

发布于 2024-12-06 17:34:23 字数 2131 浏览 1 评论 0原文

我有一个 .h 文件,其中包含以下内容:

#ifndef CS240_LINKED_LIST_H
#define CS240_LINKED_LIST_H

#include <string>

//! LLNode implements a doubly-linked list node
class LLNode {
        friend class LinkedList;
    public:
    
        LLNode(const std::string & v, LLNode * p, LLNode * n) :
          value(v), prev(p), next(n)
        {
        }
    
    private:
        std::string value;        //!< value stored in the node
        LLNode * prev;            //!< pointer to previous node in the list
        LLNode * next;            //!< pointer to next node in the list
};


//! LinkedList implements a doubly-linked list
class LinkedList
{
    public:
    
        //!  No-arg constructor.  Initializes an empty linked list
        LinkedList();
    
    
        //!  Copy constructor.  Makes a complete copy of its argument
        LinkedList(const LinkedList & other);
    
    private:
        //!  two dummy nodes to keep track of the beginning and end of the list.
        LLnode beginning;
        LLnode end;
        int size;
};

#endif

在 cpp 文件中,我有:

#include "LinkedList.h"

LinkedList::LinkedList(){
    this->beginning.prev = NULL;
    this->beginning.next = this->end;
    this->end.prev = this->beginning;
    this->end.next = NULL;
}

这是输出:

>g++ -o LinkedList.o LinkedList.cpp
In file included from LinkedList.cpp:1:
LinkedList.h:37: error: 'LLnode' does not name a type
LinkedList.h:38: error: 'LLnode' does not name a type
LinkedList.cpp: In constructor 'LinkedList::LinkedList()':
LinkedList.cpp:4: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:5: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:5: error: 'class LinkedList' has no member named 'end'
LinkedList.cpp:6: error: 'class LinkedList' has no member named 'end'
LinkedList.cpp:6: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:7: error: 'class LinkedList' has no member named 'end'

我不知道如何解决此问题。我还能如何设置开始和结束对象?如您所知,我是一名正在学习 C++ 的 Java 程序员。

I have a .h file with this in it:

#ifndef CS240_LINKED_LIST_H
#define CS240_LINKED_LIST_H

#include <string>

//! LLNode implements a doubly-linked list node
class LLNode {
        friend class LinkedList;
    public:
    
        LLNode(const std::string & v, LLNode * p, LLNode * n) :
          value(v), prev(p), next(n)
        {
        }
    
    private:
        std::string value;        //!< value stored in the node
        LLNode * prev;            //!< pointer to previous node in the list
        LLNode * next;            //!< pointer to next node in the list
};


//! LinkedList implements a doubly-linked list
class LinkedList
{
    public:
    
        //!  No-arg constructor.  Initializes an empty linked list
        LinkedList();
    
    
        //!  Copy constructor.  Makes a complete copy of its argument
        LinkedList(const LinkedList & other);
    
    private:
        //!  two dummy nodes to keep track of the beginning and end of the list.
        LLnode beginning;
        LLnode end;
        int size;
};

#endif

In a cpp file I have:

#include "LinkedList.h"

LinkedList::LinkedList(){
    this->beginning.prev = NULL;
    this->beginning.next = this->end;
    this->end.prev = this->beginning;
    this->end.next = NULL;
}

Here's the output:

>g++ -o LinkedList.o LinkedList.cpp
In file included from LinkedList.cpp:1:
LinkedList.h:37: error: 'LLnode' does not name a type
LinkedList.h:38: error: 'LLnode' does not name a type
LinkedList.cpp: In constructor 'LinkedList::LinkedList()':
LinkedList.cpp:4: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:5: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:5: error: 'class LinkedList' has no member named 'end'
LinkedList.cpp:6: error: 'class LinkedList' has no member named 'end'
LinkedList.cpp:6: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:7: error: 'class LinkedList' has no member named 'end'

I don't know how to fix this. How else would I set the beginning and end objects? Just so you know, I'm a Java programmer learning C++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耳根太软 2024-12-13 17:34:23
  • 您将 LLNode 误拼为 LLnode
  • 您需要向 LLNode 类添加一个默认构造函数,
  • 您需要获取 LinkedList 构造函数中的 this->beginningthis->end 成员的地址:

LinkedList::LinkedList(){
    this->beginning.prev = NULL;
    this->beginning.next = &this->end;
    this->end.prev = &this->beginning;
    this->end.next = NULL;
}
  • You have misspelt LLNode as LLnode.
  • You need to add a default constructor to the LLNode class
  • you need to take the address of the this->beginning and this->end members in the LinkedList constructor:

.

LinkedList::LinkedList(){
    this->beginning.prev = NULL;
    this->beginning.next = &this->end;
    this->end.prev = &this->beginning;
    this->end.next = NULL;
}
往昔成烟 2024-12-13 17:34:23

您需要声明构造函数。尝试:

class LinkedList
{
    private:
        LLnode beginning;
        LLnode end;
    public:
        LinkedList();
};

You need to declare the constructor. Try:

class LinkedList
{
    private:
        LLnode beginning;
        LLnode end;
    public:
        LinkedList();
};
最后的乘客 2024-12-13 17:34:23

您在类声明中没有 LinkedList::LinkedList() 声明。

应该给你一个不同的错误; g++ 给了我

error: definition of implicitly-declared ‘LinkedList::LinkedList()’

你确定你已经发布了你正在编译的准确代码吗?将代码缩小到小而完整的范围,然后将确切的源文件复制并粘贴到您的问题中。 (例如,没有 LLnode 的声明)。向我们展示一些我们可以自己尝试的东西。

You don't have a declaration of LinkedList::LinkedList() in the class declaration.

That should give you a different error; g++ gave me

error: definition of implicitly-declared ‘LinkedList::LinkedList()’

Are you sure you've posted the exact code you're compiling? Narrow your code down to something small but complete, and copy-and-paste the exact source files into your question. (For example, there's no declaration of LLnode). Show us something we can try ourselves.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文