嘗試創建使用LinkedList
和Node
類的鏈接列表時遇到了一些問題。在List
類中,如何創建head
,curr
和temp
對象?我認爲我可以將它們初始化爲對象,然後它會調用默認的Node()
構造函數,併爲它們分配一個數據和指針變量。但我得到的錯誤:‘Node’ does not name a type
和‘head’ was not declared in this scope
對象head
,curr
和temp
。 這裏是我的代碼:錯誤:未命名類型(C++)
LinkedList.cpp:
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
LinkedList::LinkedList() {
head = NULL;
curr = NULL;
temp = NULL;
cout << "Blank list created." << endl;
}
LinkedList::LinkedList(value_type addData) {
Node n(addData);
}
LinkedList.h:
class LinkedList {
public:
typedef std::string value_type;
LinkedList();
LinkedList(value_type addData);
private:
Node head;
Node curr;
Node temp;
};
Node.cpp:
#include <iostream>
#include <cstdlib>
#include "Node.h"
using namespace std;
Node::Node() {
nodePtr n = new node;
n->next = NULL;
n->data = NULL;
}
Node::Node(value_type addData) {
nodePtr n = new node;
n->next = NULL;
n->data = addData;
}
Node.h:
class Node {
public:
typedef std::string value_type;
Node();
Node(value_type addData);
private:
struct node {
value_type data;
node* next;
};
typedef struct node* nodePtr;
};
任何幫助將不勝感激,謝謝你們!
將'#include「Node.h」'添加到'LinkedList.h'。 – songyuanyao