C ++类没有名为'blah'的成员

时间:2011-09-24 20:01:05

标签: c++ header private multifile

我有一个.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程序员。

3 个答案:

答案 0 :(得分:4)

  • 您错误拼写LLNodeLLnode
  • 您需要向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;
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

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

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