2016-04-29 36 views
0

我有一個鏈表類,爲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://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-文件無論是否解決您的眼前的問題,它都會解決很多未來的問題。 – user4581301

+0

現在我已經閱讀了代碼,這裏有很多原因不能解決這個問題。相反,我會提出一些建議。該清單非常瞭解它包含的內容。這使得很難判斷一個bug是在列表邏輯還是在數據輸入和處理邏輯中,特別是考慮到列表是模板。把它們分開。自己寫鏈接列表。測試它的廢話,所以你知道你可以添加,刪除,查找和通過它沒有錯誤。然後編寫單獨的新類,讀取並保存文件數據。 – user4581301

回答

0

我想我發現了這個錯誤。第一次運行添加方法頭設置,但第二次嘗試設置第二個值。你有這樣的代碼

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

不客氣!如果這有助於標記回答正確 –