簡體   English   中英

C ++ Simple LinkedList超出范圍的變量

[英]C++ Simple LinkedList out of scope variables

我正在嘗試創建一個LinkedList ,它接收用戶信息並將其存儲到其節點中。 之后將顯示用戶信息。 我已經通過這個工作了,這就是我所擁有的:

    #include <iostream>
    using namespace std;

    template <typename T>
    class Node
    {
    T data;
    Node* next;
};

template <typename T>
class LinkedList

{
    public:
    //Default constructor
    LinkedList(){
        head = NULL;
        tail = NULL;
    }

    void addData(T val){
        Node* newNode = new Node;
        newNode->data = val;
        newNode->next = NULL; // we add newNode at end of linked list
        if( head == NULL){
            head = newNode;
            tail = newNode;
        }

        else{
            tail ->next = newNode;
            tail = newNode;
        }
    }

    void printData() {
        for(Node* i = head; i!=NULL; i = i -> next){
            cout << i->data << " " ; 

        }
    }

    private:
    //Declare the head and tail of the LinkedList
        Node* head;
        Node* tail;

};

int main(){

    LinkedList<string> usrInput;
    cout << "Enter a number or word : ";
    string input ;
    cin >> input;

    usrInput.addData(input);
    usrInput.printData();



    LinkedList<int> numbers;
    while (true)
    {
        cout<< "Enter a number (-1 to stop): ";
        int num;
        cin >> num;

        if (num == -1)
            break;

        numbers.addData(num);
    }
    numbers.printData();


}

問題是,當我編譯程序時,遇到一堆錯誤,這些錯誤指的是超出范圍的成員變量。 成員變量不應該是私有的嗎?


這就是調試器的功能:

46:9: error: invalid use of template-name 'Node' without an argument list

47:9: error: invalid use of template-name 'Node' without an argument list

 In constructor 'LinkedList<T>::LinkedList()':

18:9: error: 'head' was not declared in this scope

19:9: error: 'tail' was not declared in this scope

 In member function 'void LinkedList<T>::addData(T)':

23:13: error: missing template arguments before '*' token

23:15: error: 'newNode' was not declared in this scope

23:29: error: invalid use of template-name 'Node' without an argument list

26:13: error: 'head' was not declared in this scope

28:13: error: 'tail' was not declared in this scope

32:13: error: 'tail' was not declared in this scope

 In member function 'void LinkedList<T>::printData()':

38:17: error: missing template arguments before '*' token

38:19: error: 'i' was not declared in this scope

38:23: error: 'head' was not declared in this scope

默認情況下,類的數據成員是私有的。 默認情況下,它們在結構上是公共的。 也就是說,在:

class Node
    {
    T data;
    Node* next;
};

LinkedList類無法查看Node的成員。 嘗試使用以下任一方法:

struct Node {
    T data;
    Node* next;
};

要么

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

從風格上講,大多數實際的實現將Node嵌套為LinkedList類中的私有成員結構。

單獨的Node在代碼中沒有引用有效的類型。 您需要使用Node<T>

當編譯器遇到Node會將其標記為非法使用,並丟棄使用該類型定義的變量或成員,從而在稍后使用該變量或成員時在編譯中引起其他錯誤。

切換為對Node<T>使用完整類型后,您將遇到另一個答案中提到的問題,即Node<T>類成員的私有成員身份。

我希望即使我遲到了哈哈,我的回答也會對其他人有幫助:):

   #include <iostream>
   using namespace std;

 template <typename Type>
 class Node
{     
   template<typename T> friend class LinkedList;/* you need to make your linkedList 
        class a friend variable to be able to,acces the private members directly from the objects inside the class Linkedlist.
        other option its to make them public, but it is not recomended and other option si to make functions to acces de data, but this will be very messy, just make your linkedlist class friend*/
   Type data;
   Node<Type>* next; // you need to specify which data type the node will be
};

 template <typename T>
 class LinkedList
{
 public:
//Default constructor
LinkedList(){
    head = NULL;
    tail = NULL;
}

 void addData(T val){
    // you can't forget to specify the data type since it is a    template!!
    Node<T>* newNode = new Node<T>();
    newNode->data = val;
    newNode->next = NULL; // we add newNode at end of linked list
    if( head == NULL){
        head = newNode;
        tail = newNode;
    }

    else{
        tail ->next = newNode;
        tail = newNode;
    }
 }

 void printData() {
    // again specify the data type of the template
    for(Node<T>* i = head; i !=NULL; i = i -> next){
        cout << i->data << "\n" ;

    }
}

private:
    //Declare the head and tail of the LinkedList
    Node<T>* head;// Again specify the data type of the template
    Node<T>* tail;// here the same thing

};

int main(){

   LinkedList<string> usrInput;
   cout << "Enter a number or word : ";
   string input ;
   getline(cin,input);// use the get line function to get strings from standar input

   usrInput.addData(input);
   usrInput.printData();

   // Declare the num variable otuside and initialize it, you should always initialize the variables
   int num(0);
   LinkedList<int> numbers;
   while (true)
  {
       cout<< "Enter a number (-1 to stop): ";
      // you need to validate the input if you don't want to run an infinite loop
       if(!(cin >> num)){
           cin.clear();
           cin.ignore(100,'\n');
        }
        else {
        // if we succes with the imput you can add the data to the linked list :)
           if (num == -1){break;}
           numbers.addData(num);
        }
   }
      numbers.printData();


  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM