my node class for a LLL, and a class derived from it
class node
{
public:
node();
~node();
node *& getNext();
protected:
node * next;
};
class word: public node
{
public:
word();
~word();
protected:
char ** words; //an array of char pointers
};
and I have another class
class sentence
{
public:
sentence();
~sentence();
void insert(node *& head, char *& word_to_add);
sentence *& operator+=(char *& sentence_to_add);
void remove(char *& word_to_remove);
void testing(sentence *& s);
void display();
void removeAll();
protected:
node * head;
};
and here's my implementation of the insert function
void sentence::insert(node *& head, char *& word_to_add)
{
if(!head)
{
head = new word;
insert(head, word_to_add);
}
else
{
for(int i = 0; i < MAX_WORD; ++i)
{
if(!head->words[i])
{
head->words[i] = new char[strlen(word_to_add)+1];
strcpy(head->words[i], word_to_add);
cout << head->words[i] << endl;
return;
}
}
insert(head->getNext(), word_to_add);
}
}
I'm getting the error class node has no data member named words. But if I were to change the prototype void insert(node *& head, char *& word_to_add); into void insert(word *& head, char *& word_to_add); it would say that word has no data member next. I'm kinda stuck because I did this in a previous program and it worked just fine. I was wondering what I'm doing wrong, so if someone could point that out, that'd be great!
edit: I forgot to include that I'm using the friend keyword in class word. For testing purposes. friend class sentence;
I would not derive from the node class.
Have the node be generic and contain pointer to some data (word) instead of having the data directly in the node ?
class node
{
node* next;
word data;
};
class word
{
char** words;
};
Since you tagged this C++, I might suggest you use a standard C++ collection (std::vector) instead of a linked list.
Related
Here is a simple c++ class for binary tree. Compiler throws an error:
E0147 declaration is incompatible with "void BinaryTree::getLeftChild(node *n)"
Here node is a struct defined under the private section in the class. I am not sure why it says incompatible declaration.
//------------------------ BinaryTree class-----------------
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void createRootNode();
void getChildren();
void getLeftChild(node* n);
void getRightChild(node* n);
private:
typedef struct node
{
node *lchild = nullptr;
int data;
node *rchild = nullptr;
}node;
queue <node*> Q;
node *root;
};
BinaryTree::BinaryTree()
{
createRootNode();
getChildren();
}
void BinaryTree::createRootNode()
{
root = new node();
cout << "Enter value for root node" << endl;
cin >> root->data;
Q.push(root);
}
void BinaryTree::getChildren()
{
while (Q.empty == false)
{
getLeftChild(Q.front());
getRightChild(Q.front());
Q.pop();
}
}
void BinaryTree::getLeftChild(node* n)
{
}
void BinaryTree::getRightChild(node* n)
{
}
Code picture with errors
I got another struct in global scope declared as "node" which created chaos. Secondly, i also need to fix the order of public and private sections.
Here is working code
//------------------------ BinaryTree class-----------------
class BinaryTree
{
private:
typedef struct node
{
node *lchild = nullptr;
int data;
node *rchild = nullptr;
}node;
queue <node*> Q;
node *root;
public:
BinaryTree();
~BinaryTree();
void createRootNode();
void getChildren();
void getLeftChild(node* n);
void getRightChild(node* n);
};
BinaryTree::BinaryTree()
{
createRootNode();
getChildren();
}
void BinaryTree::createRootNode()
{
root = new node();
cout << "Enter value for root node" << endl;
cin >> root->data;
Q.push(root);
}
void BinaryTree::getChildren()
{
while (Q.empty() == false)
{
getLeftChild(Q.front());
getRightChild(Q.front());
Q.pop();
}
}
void BinaryTree::getLeftChild(node* n)
{
}
void BinaryTree::getRightChild(node* n)
{
}
First error, is that you need to forward declare the node.
Second error, is that you are trying to access node which is privately declared inside of BinaryTree.
First answer:
typedef struct node
{
node* lchild = nullptr;
int data;
node* rchild = nullptr;
}node;
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void createRootNode();
void getChildren();
void getLeftChild(node* n);
void getRightChild(node* n);
private:
node* root;
};
void BinaryTree::getLeftChild(node* n)
{
}
void BinaryTree::getRightChild(node* n)
{
}
Now code compiles fine.
Or if you want to have the typedef defined as private inside, you need the implementation to be inside the class as well.
Second Answer:
typedef struct node;
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void createRootNode();
void getChildren();
void getLeftChild(node* n)
{
}
void getRightChild(node* n)
{
}
private:
typedef struct node
{
node* lchild = nullptr;
int data;
node* rchild = nullptr;
}node;
node* root;
};
So I have the class LinkedList. Can someone explain why node * head is outside of the struct and node * next, prev are inside the struct? What is the purpose of this and how does it differ from the others? thanks
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
void display(
private:
struct node
{
char data;
node * next;
node * prev;
};
node *head;
};
This
struct node
{
char data;
node * next;
node * prev;
};
is an inner declaration of a structure type within a class.
This
node *head;
is a declaration of a data member of the type struct node * of the class LinkedList. To make it more clear rewrite the list definition the following way
struct node
{
char data;
node * next;
node * prev;
};
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
void display(
private:
node *head;
};
Declaring the structure inside the class definition as a private declaration makes the structure type invisible for the users of the linked list.
I created a private static variable that keeps track of the number of elements in the linked list.
struct node
{
int data;
node *next;
};
class linkedList
{
private:
node *head,*tail;
static int listSize;
public:
linkedList()
{
head=NULL;
tail=NULL;
}
void insert(int n)
{
node *temp=new node;
temp->data=n;
temp->next=NULL;
if(head == NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
linkedList::listSize+=1;
}
};
void main()
{
linkedList l;
l.insert(10);
l.insert(20);
}
The compiler throws an error when it reaches the line linkedList::listSize+=1;
error: ‘linkedList’ has not been declared.
Once your typos corrected (inser(20) instead of insert(20) and : instead of ; in linkedList(), your program almost compiles.
There is just one thing missing: you need to implement the listSize variable somewhere for example by putting int linkedList::listSize; before main:
...
int linkedList::listSize; /(/ <<< add this
void main()
{
linkedList l;
l.insert(10);
l.insert(20);
}
But why are you using a static variable for counting the elements of the list? You probably want listSize to be an ordinary (non static) class member, just as head and tail:
class linkedList
{
private:
node * head, *tail;
int listSize; // no static
public:
...
and drop the int linkedList::listSize; suggested before.
I'm trying to create a spell checking program in C++ by reading in a dictionary from a .txt file. I've got the read in function working perfectly fine, the issue I'm coming across is when I try to navigate and add to my linked list.
When I try to set the pointer of the newest node to add, to the value of the head pointer, I'm getting an error stating No viable conversion from 'Node' to 'Node *'.
What is the best way to perform this conversion.
I've already tried turning my 'Node Head;' inside of my linked list class to a pointer but receive the same error.
To start I created my Node struct (Declared in a header file)
struct Node
{
private:
std::string word;
Node *nextNode;
public:
//Default constructor
Node();
~Node();
//My Setters and getters for the class
void setWord(std::string _word) { word = _word; }
std::string getWord() { return word; }
void setNode(Node *_nextNode) { nextNode = _nextNode; }
Node getNode() { return *nextNode; }
};
Followed by my LinkedList Class (Also declared in a Header file)
class LinkedList
{
private:
Node head;
int listSize;
public:
LinkedList();
~LinkedList();
void setListSize(int _listSize) { listSize = _listSize; }
int getListSize() { return listSize; }
void setHead(Node _head) { head = _head; }
Node getHead() { return head; }
//Function that adds the next node to the head
void addToHead(LinkedList &myList, Node &myNode);
};
Heres my Function
void LinkedList::addToHead(LinkedList &myList, Node &myNode)
{
myNode.setNode(myList.getHead().getNode());
//Here is where I'm getting my error
//"No viable conversion from 'Node' to 'Node *'
myList.setHead(myNode);
}
The LinkedList class shouldn't own the first Node.
The member head should be a Node* width default value nullptr (the list is empty).
listSize should also have a default value assigned.
LinkedList() head(nullptr), listSize(0) {};
Edit
Personally I would avoid to force the external code to manage the single nodes.
Keep an implementation independent interface.
class LinkedList
{
private:
Node *head_;
int size_;
public:
LinkedList();
~LinkedList();
int size() const { return listSize; }
// insert after the i-th element
void insert(std::size index, std::string const& word);
// return the i-th element
std::string &at(std::size index);
std::string const &at(std::size index) const;
// removes the i-th element
void remove(size::size index);
};
In this way you centralize all list manipulation code into the LinkedList class.
You should also consider problems related to copying a LinkedList object.
class Newstring
{
public:
Newstring();
void inputChar ( char);
void display ();
int length ();
void concatenate (char);
void concatenate (Newstring);
bool substring (Newstring);
void createList ();
Node * getHead (); // error
private:
struct Node
{
char item;
Node *next;
};
Node *head;
};
I am getting a syntax error : missing ';' before '*' on the declaration for my getHead function (yes I can't think of a better name). The purpose of this function is to return the head pointer.
Declare Node before you use it.
You have to declare the Node struct above getHead();
class Newstring
{
public:
struct Node
{
char item;
Node *next;
};
Newstring();
void inputChar ( char);
void display ();
int length ();
void concatenate (char);
void concatenate (Newstring);
bool substring (Newstring);
void createList ();
Node * getHead (); // error
private:
Node *head;
};
To answer Brandon about keeping the struct in private, or keeping the current code while adding a declaration, the way is :
class Newstring
{
struct Node; // here the declaration
public:
Newstring();
void inputChar ( char);
void display ();
int length ();
void concatenate (char);
void concatenate (Newstring);
bool substring (Newstring);
void createList ();
Node * getHead (); // error
private:
struct Node
{
char item;
Node *next;
};
Node *head;
};
Node * getHead()
Compiler is not able to get the definition of Node when getHead() is encountered.
struct Node
{
char item;
Node *next;
};
Put the definition of Node before it is used.
class Newstring
{
private:
struct Node
{
char item;
Node *next;
};
Node *head;
public:
Newstring(); ...
Node * getHead ();
Another way would be to forward declare Node by placing a struct before Node
:
void createList ();
struct Node * getHead ();
private:
struct Node
{
: