我有一个链表类,它使用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 :(得分:0)
我想我发现了错误。第一次运行add方法头设置,但第二次尝试设置第二个值。你有这个代码
first = this->head;// this is the first element
second = this->head->getNext(); // this is null (hast been assign yet
然后你进入for循环,在第一个&#34;否则如果&#34;声明你有这个:
else if(first->getID() < newNode->getID() && second->getID() > newNode->getID())
当你说第二个&gt; getID()时,你说null-&gt; getID()会导致分段错误。
我希望这可以解决您的问题。祝你好运!