C++ class has no member named 'blah'
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;
LL开发者_高级运维node 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++.
- You have misspelt
LLNode
asLLnode
. - You need to add a default constructor to the LLNode class
- you need to take the address of the
this->beginning
andthis->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;
}
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.
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.
精彩评论