简体   繁体   English

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

[英]C++ class has no member named 'blah'

I have a .h file with this in it: 我有一个.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

In a cpp file I have: 在一个cpp文件中,我有:

#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++. 就像您知道我是学习C ++的Java程序员一样。

  • You have misspelt LLNode as LLnode . 您将LLNode拼错了LLnode
  • You need to add a default constructor to the LLNode class 您需要向LLNode类添加默认构造函数
  • you need to take the address of the this->beginning and this->end members in the LinkedList constructor: 您需要在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 need to declare the constructor. 您需要声明构造函数。 Try: 尝试:

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

You don't have a declaration of LinkedList::LinkedList() in the class declaration. 您在类声明中没有LinkedList::LinkedList()声明。

That should give you a different error; 应该给你一个不同的错误。 g++ gave me g ++给了我

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 ). (例如,没有LLnode的声明)。 Show us something we can try ourselves. 向我们展示一些我们可以尝试的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM