获取链表上的"expected ‘)’ before ‘&’ token"到 BST 构造函数签名



对于我的家庭作业,我有一些头文件(由讲师提供)存在一些问题。目标是从处理BST和链表的头文件中实现方法(从列表构建树,从树构建列表和其他一些方法)。

问题:从标头的签名中得到一些错误,包括:

错误:在"&"之前应为")"代币btree(LinkedList&list);*我不知道为什么

错误:"LinkedList"未命名类型btree&运算符=(const LinkedList&ls);*尽管我已经在这个文件中包含了链接列表标题

  • 错误:"LinkedList"未命名类型LinkedList*树linkListbyDepth()

编辑:我删除了循环,包括frindLinkedList行,并将类btree和structnode添加到LinkedList.h中。我还添加了cpp文件。请忽略cpp文件中的错误。我知道他们有一些错误。我并不是想让你们帮我解决这个问题。我只是想修复头错误,这样我就可以开始在cpp文件上实现方法,测试它们,并在做的时候学习cpp。(这是我的第一个cpp作业)

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

链接列表标题:

#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文件:

#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;
}

链接列表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;
}

显然您的代码在之前没有声明LinkedList

btree(LinkedList &list);

名称查找找不到该名称。因此出现了错误。

你的朋友声明高于

friend class LinkedList;

没有引入LinkedList作为常规名称查找可见的名称。

我看到你包括LinkedList.h。里面有什么?你在那个头文件中声明了LinkedList吗?如果是这样的话,那么它可能会遇到循环包含问题。没有办法不看LinkedList.h的内容。


现在,当您发布LinkedList.h时,很明显您有循环包含。这就是你犯错误的原因。你必须去掉循环包含。删除其中一个形成循环包含的#include指令,并转发声明相应的类。

例如:

  • LinkedList.h中删除#include "btree.h"
  • 相反,添加转发声明

    struct node;
    class btree;
    

    至CCD_ 10。

  • #include "btree.h"添加到LinkedList.cpp

LinkedList声明为LinkedList的朋友是毫无意义的。

最新更新