【问题标题】:C++ Simple LinkedList out of scope variablesC ++简单LinkedList超出范围变量
【发布时间】:2017-01-18 04:17:30
【问题描述】:

我正在尝试创建一个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

【问题讨论】:

    标签: c++ scope linked-list singly-linked-list


    【解决方案1】:

    类的数据成员默认是私有的。默认情况下,它们在结构上是公共的。也就是说,在:

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

    LinkedList 类对节点的成员没有可见性。尝试使用:

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

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

    从风格上讲,大多数实际实现将 Node 作为私有成员结构嵌套在 LinkedList 类中。

    【讨论】:

    • 谢谢!不是本机 C++ 开发人员。同样的蛋糕只是口味不同。这解决了我的问题。
    • N.P.考虑将您的问题标记为已回答未来的人
    • 亲爱的tegtmeye,我如何将问题标记为已回答?我是新来的:D。我还必须标记我的另一个涉及 asyncTask 的问题,所以请告诉我。
    【解决方案2】:

    Node 本身并不引用代码中的有效类型。您需要使用Node&lt;T&gt;

    当编译器遇到Node 时,它会将其标记为非法使用并丢弃使用该类型定义的变量或成员,从而在您使用该变量或成员时在编译中导致其他错误。

    一旦您切换到使用带有Node&lt;T&gt; 的完整类型,您将遇到另一个答案中提到的问题,即Node&lt;T&gt; 类成员的私人成员资格。

    【讨论】:

    • 谢谢我已经解决了第 46 行和第 47 行的类型不匹配参数。
    • 我现在的问题是我的变量范围。为什么它们超出范围。
    • @ErickRamirez 你用 Node 替换了 Node 的所有用法吗?
    • Mikel F 我已经成功编译了我的程序。它运行。我不是本地 C++ 开发人员,但我有其他编程语言的经验。感谢您所有的帮助!我还有一个额外的问题,我是否编辑我的代码以反映正确的版本?对于未来的用户?或者就这样让他们理解。
    • @ErickRamirez 你应该保持原样,这样问题才有意义。
    【解决方案3】:

    我希望我的回答对其他人也有帮助,即使我迟到了哈哈 :) :

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

    【讨论】:

      猜你喜欢
      • 2016-04-30
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 2012-11-20
      • 2020-12-24
      相关资源
      最近更新 更多