-1

For my homework, I've got some headers files (supplied by my lecturer) with some issues. The goal - implementing the methods from the header files that dealing with BST and linked lists (building a tree from a list, a list from a tree and some other methods).

The problem: getting some errors from the signatures of the header, including:

error: expected ‘)’ before ‘&’ token btree(LinkedList &list); * I don't have a clue why

error: ‘LinkedList’ does not name a type btree & operator=(const LinkedList & ls); * althoght I've included the linked list header in this file

  • error: ‘LinkedList’ does not name a type LinkedList* Tree2linkListbyDepth();

EDIT: I removed the circule including, the frind LinkedList line, and add class btree and struct node to linkedList.h. I add the cpp files as well. Please ignore errors at the cpp files. I know they have some mistakes. And I'm not trying to make you guys solve it for me. I'm only tring to fix the headers errors so that I can start implement the methods on the cpp files, test them and learn cpp while doing it. (This is my first cpp homework)

btree h:

#ifndef _btree_H
#define _btree_H

#include <iostream>

using namespace std;
struct node
{
    int key_value;
    node *left;
    node *right;
};

class btree
{
friend class LinkedList;
node* root;

public:
// Default constructor
btree();
~btree();

// Copy Constructor by list
btree(LinkedList & list);
// Copy Constructor by tree
btree(btree & bt);
// assignment operator from linked list
btree & operator=(const LinkedList & ls);
// assignment operator from tree
btree& operator=(const btree &bt);
// insert new value to binary tree
void insert(int key);
// mirror the tree
void mirror();
LinkedList* Tree2linkListbyDepth();
int getTreeDepth();

// print tree (in order)
friend ostream& operator<<(ostream& os, btree& dt);

};

#endif

Linkedlist Header:

#ifndef _List_H
#define _List_H


#include<iostream>

class btree;
struct node;

using namespace std;

class Node
{
public:
Node* next;
int data;
};


class LinkedList
{
friend class btree;

public:
int length;
Node* head;
LinkedList(btree &bt);
LinkedList(LinkedList &bt);
LinkedList();
~LinkedList();
void add(int data);
LinkedList & operator=(const LinkedList & bt);
LinkedList& operator=(const btree &bt);
friend ostream& operator<<(ostream& os, LinkedList& l);
LinkedList inorder(node *bt, LinkedList *head);
Node inOrder(node *root, LinkedList *list) ; //change to private!
};

#endif

btree cpp file:

#include "btree.h"
#include "LinkedList.h"
#include <iostream>

btree::btree() { // default constructor  CHECK!
root->right = nullptr;
root->left = nullptr;
root->key_value = 0; // check if needed
}


btree::btree(LinkedList & list)
{
Node* temp = list.head;
while(temp != nullptr) {
    insert(temp->data);
    temp = temp->next;
   }
}

node *newNode(int key) { // create a new node
node *temp = new node; //CHECK IF NEW IS NEEDED
temp->key_value = key;
temp->left = temp->right = NULL;
return temp;
}

node *insert(node* node, int key) {  // NEED FIXING
// If the tree is empty, return a new node
if (node == NULL)
    return newNode(key);

/* Otherwise, recur down the tree */
if (key < node->key_value)
    node->left = insert(node->left, key);
else if (key > node->key_value)
    node->right = insert(node->right, key);

/* return the (unchanged) node pointer */
return node;
}


btree::~btree() { // CHECK !
}



void inorder(node *root) { // CHECK
    if (root != NULL) {
        inorder(root->left);
        printf("%d \n", root->key_value);
        inorder(root->right);
    }
}

// passing the root to a printing function
ostream & operator<<(ostream & os, btree & dt) { // CHECK
inorder(dt.root);
return os;
}

linkedlist cpp:

#include "LinkedList.h"
#include "btree.h"
#include <iostream>

LinkedList::LinkedList(btree & bt) {
    LinkedList *list = new LinkedList;
    inOrder(bt.root, list);

}

Node inOrder(node *root, LinkedList *list) { // CHECK
    if (root->key_value != NULL) {
        inOrder(root->left, list);
        list->head->next = new Node;
        list->head = list->head->next;
        inOrder(root->right, list);
    }
}

LinkedList::LinkedList() {
head = new Node;
    head->next == NULL;
}

LinkedList::~LinkedList()
{
    delete head;
}
Liran
  • 1
  • 2
  • `btree(LinkedList &list);` Try using a different name for your parameter. Because of `using namespace std;` it may be seeing the `std::list` class. – Retired Ninja Jan 26 '19 at 07:41
  • What's inside your `"LinkedList.h"`? Post the contents, including the `#include` directives that you have there. – AnT stands with Russia Jan 26 '19 at 07:51
  • just tried a different name for the list parameter, getting the same error still. I'm editing and adding the second header file. – Liran Jan 26 '19 at 07:57
  • Removing the linked list header and declaring a forward decl before class `btree` compiles fine, so you either have a likely circular inclusion or an include-guard repetition. Unrelated, [`using namespace std;` is a terrible idea](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), especially in header files. And the implementation has reservation for identifiers beginning with two underscores or one underscore followed by a capital letter, so while `_btree_H` is viable, `_List_cpp` (an odd name for a header fencepost on its own) is to be avoided. – WhozCraig Jan 26 '19 at 08:05
  • You have circular includes – user7860670 Jan 26 '19 at 08:09
  • Unrelated, I can't be the only one curious about where `node` comes from, when `Node` is clearly the defined class name. As near as I can see, there is no `node` by declaration *anywhere*. – WhozCraig Jan 26 '19 at 08:14
  • @Liran A binary tree and a linked list are two totally different data structures. Why are you trying to merge the two totally different concepts into one? That is conceptually where everything breaks down -- it isn't really a coding issue, it seems to be a conceptual one. You should be able to write a linked list class without any knowledge of a binary tree, and you should be able to write a binary tree class with no knowledge of a linked list. – PaulMcKenzie Jan 26 '19 at 10:18
  • @WhozCraig I just edit the question and add the cpp files. The node is the tree's struct and Node is the list class. – Liran Jan 27 '19 at 09:54
  • @PaulMcKenzie I know it looks wired. But it's my home assignment, part of my software engineering degree.. I've got the header files as they are from the lecturer, and I can only add private methods to the headers and inclusion. I can't change the struct, classes and signatures of the headers. My assignment is to implement the method mentioned at the header files and fix the headers. – Liran Jan 27 '19 at 10:03

1 Answers1

1

Apparently you code does not declare LinkedList before

btree(LinkedList &list);

Name lookup cannot find that name. Hence the error.

The friend declaration that you have above

friend class LinkedList;

does not introduce LinkedList as a name visible to regular name lookup.

I see that you include LinkedList.h. What's in there? Do you declare LinkedList in that header file? If so, then it might be suffering from circular inclusion problem. No way to say without seeing the contents of LinkedList.h.


Now as you posted LinkedList.h, it is obvious that you have circular inclusion. That's the reason for your error. You have to get rid of circular inclusion. Remove one of these #include directives that form circular inclusion and forward-declare the corresponding class instead.

For example:

  • Remove #include "btree.h" from LinkedList.h
  • Instead add forward-declarations

    struct node;
    class btree;
    

    to LinkedList.h.

  • Add #include "btree.h" to LinkedList.cpp.

Declaring LinkedList as a friend to LinkedList is pointless.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Circular inclusion would be my guess, incredibly confusing the first time it happens to you. – john Jan 26 '19 at 07:58
  • just add the second header file – Liran Jan 26 '19 at 08:01
  • I removed the inclusion from the Linkedlist header and add the forward-declaration: class btree, and now I've got a different type of errors for each line that uses "node". For example: error: ‘node’ has not been declared LinkedList inorder(node *bt, LinkedList *head); – Liran Jan 26 '19 at 09:10
  • and I keep getting the wired error: error: expected ‘)’ before ‘&’ token btree(LinkedList &list); – Liran Jan 26 '19 at 09:43
  • @Liran: A forward declaration for `struct node;` in `LinkedList.h` is also needed. As for the rest... there's no way to say what's going on there without seeing the result of your recent changes. – AnT stands with Russia Jan 26 '19 at 15:59