类链接列表模板运行时错误

问题描述:

我有一个链表类,为Node类和List类使用模板。我做了一些调试,得出的结论是,在我的Node类中,成员函数不能访问成员数据,但构造函数可以。我想知道我该如何解决这个问题!类链接列表模板运行时错误

#include <iostream> 
#include <fstream> 
#include <string> 
#include "List.h" 


using namespace std; 


int main() 
{ 
ifstream fileIn("data1.txt"); 

List<int> studentList; 

if(fileIn.fail()) 
    cout << "file did not open" << endl; 
else 

    studentList.add(fileIn); 




fileIn.close(); 
cin.get(); 
cin.ignore(); 
return 0; 

}

//List.h //忽略的注释方法,还没有写他们。

#ifndef LIST_H 
#define LIST_H 

#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include "Node.h" 

using namespace std; 

template <class NumType> 
class List 
{ 

int counter; 

bool isEmpty(); 
const bool print(){ 
} 

public: 

Node<NumType> * head; 

    List() 
    { 

     this->head = NULL; 
     counter = 0; 
    } 

    ~List() 
    { 

    } 


    //place in an order thart keeps the array in ascending order 
    void add(ifstream &); 
/* 
    Node* location(Node *){ 
    } 

    bool remove(const int){ 
    } 

    bool clear(){ 
    } 

    const void peek(const Node*){ 
    } 

    //average of all test scores or just one students test scores? 
    float average(){ 
    } 

    char grade(){ 
    } 
*/ 
}; 



#include "List.cpp" 
#endif 

//List.cpp

#include "List.h" 


using namespace std; 

template <class NumType> 
bool List<NumType> :: isEmpty() 
{ 
cout << "inside isEmpty" << endl; 
return(head == NULL); 
} 


template <class NumType> 
void List<NumType> :: add(ifstream & fin) 
{ int dummyID; 
NumType tests[3]; 
string dummyName; 


while(fin >> dummyID) 
{  fin.ignore(); 
     getline(fin, dummyName); 

     for(int x = 0; x < 3; x++) 
      fin >> tests[x]; 
     fin.ignore(); 

     cout << dummyID << endl; 
     cout <<dummyName << endl; 
     for(int y = 0; y < 3; y++) 
      cout << tests[y] << " "; 

    if(isEmpty()) 
    { 

     this->head = new Node<NumType>(NULL, tests, dummyID, dummyName); 
     counter++;cout << "inside" << endl; 
    } 
    else 
    { 
     Node<NumType> *newNode = new Node<NumType>(NULL, tests, dummyID, dummyName); 



     Node<NumType> *first = new Node<NumType>(NULL, tests, dummyID, dummyName); 
     Node<NumType> *second; 

     first = this->head; 
     second = this->head->getNext(); 

     //create location() method to handle this! 
     for(int x = 0; x < counter; x++) 
     {   

       if(first->getID() > newNode->getID()) 
       { 
        head = newNode; 

        counter++; 
        break; 
       } 
       else if(first->getID() < newNode->getID() && second->getID() > newNode->getID()) 
       { 

        newNode->setNext(second); 
        first->setNext(newNode); 

        counter++; 
        break; 
       } 
       else if(second->getID() < newNode->getID() && second->getNext() == NULL) 
       { 
        second->setNext(newNode); 

        counter++; 
        break; 
       } 
       else 
       { 
        first = second; 
        second = second->getNext(); 
       } 
     } 

    } 
} 
Node<NumType> * temp = head; 
for(int x = 0; x <= counter; x++) 
{ 
    NumType *arr; 

    cout << temp->getID() << endl << temp->getName() << endl; 
    arr = temp->getAllScores(); 
    for(int y = 0; y <3 ; y++) 
     cout << *(arr+y) << endl; 

    temp = temp->getNext(); 
} 

}

//Node.h

#ifndef NODE_H 
#define NODE_H 


#include <cstdlib> 
#include <iostream> 

using namespace std; 

template <class ItemType> 
class Node 
{ 

    static const int SIZE = 3; 
    int ID; 
    ItemType scores[SIZE]; 
    string name; 
    Node *next; 



public: 

    Node() 
    { 
     this->scores[0] = 0; 
     this->scores[1] = 0; 
     this->scores[2] = 0; 
     this->name = ""; 
     this->ID = 0; 
     this->next = NULL; 
    } 


    Node(Node * nPtr, ItemType tests[], int num, string n) 
    { 
     this->next = nPtr; 

     for(int z = 0; z < SIZE; z++) 
      this->scores[z] = tests[z]; 

     this->ID = num; 
     this->name = n; 
    } 

    ~Node(){} 


    void setNext(Node *); 

    string getName(); 

    int getID(); 


    ItemType* getAllScores(); 

    Node* getNext(); 




}; 

#include "Node.cpp" 
#endif 

Node.cpp

#include "Node.h" 
#include <string> 

using namespace std; 


template <class ItemType> 
void Node<ItemType> :: setNext(Node * nextPtr) 
    { 
     cout << "inside setNext()" << endl; 
     this->next = nextPtr; 
     cout << "exited setNext()" << endl; 
    } 

    template <class ItemType> 
    string Node<ItemType> :: getName() 
    { 
     return (this->name); 
    } 

    template <class ItemType> 
    int Node<ItemType> :: getID() 
    { 
     return (this->ID); 
    } 

    template <class ItemType> 
    ItemType* Node<ItemType> :: getAllScores() 
    { 
     return (this->scores); 
    } 

    template <class ItemType> 
    Node<ItemType> * Node<ItemType> :: getNext() 
    { 
     return (this->next); 
    } 
+0

不知道这正是你正在处理的,但给这个阅读:http://*.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-文件无论是否解决您的眼前的问题,它都会解决很多未来的问题。 – user4581301

+0

现在我已经阅读了代码,这里有很多原因不能解决这个问题。相反,我会提出一些建议。该清单非常了解它包含的内容。这使得很难判断一个bug是在列表逻辑还是在数据输入和处理逻辑中,特别是考虑到列表是模板。把它们分开。自己写链接列表。测试它的废话,所以你知道你可以添加,删除,查找和通过它没有错误。然后编写单独的新类,读取并保存文件数据。 – user4581301

我想我发现了这个错误。第一次运行添加方法头设置,但第二次尝试设置第二个值。你有这样的代码

first = this->head;// this is the first element 
second = this->head->getNext(); // this is null (hast been assign yet 

然后你去的for循环中,并在第一个“否则,如果”语句你有这样的:

else if(first->getID() < newNode->getID() && second->getID() > newNode->getID()) 

当你说二线>的getID()你正在说null-> getID()会导致分段错误。

我希望这可以解决您的问题。祝你好运!

+0

这帮了很大忙,现在一切正常,非常感谢:D –

+0

不客气!如果这有助于标记回答正确 –