几个月来,我一直在尝试自学编程,我买了大约三本书,并在 YouTube 上观看了大量视频,这些视频到目前为止都很棒。但在过去的一周左右,多亏了链表,我被难住了!我明白了整个概念,但语法让我很生气!我已经编写了一些非常简单的链表程序来练习,但是现在我正在尝试编写一个程序,在该程序中,我将文件中的一些信息(已申请签证的人的信息)加载到链表中,然后就可以了再次显示全部内容(一旦完成,我计划将其扩展到仅显示某些节点并删除某些节点的位置,但我不会详细说明)。
ps 其中一些代码来自书中的一个类似示例。
这是加载信息的主要(大部分)循环(它不起作用)
List myList;
visa_application *visa;
Node * pNode;
string visa_type;//these are all the variables that i want to load from the file
int invoice_no;
string surname;
string firstname;
double contact;
string status;
string result;
ifstream in;
in.open("applications.txt",ios::in);
while (!in.eof()){
pNode = new Node;
visa = new visa_application; //allocate memory for nodes
in >> visa-> visa_type >> visa->invoice_no >> visa-> surname;
in >> visa-> firstname >> visa-> contact >> visa-> status >> visa-> result ;
pNode->nData = &visa; //put some data in the node
myList.appendNode(pNode); //add node to list
}
这是我的类节点头文件
class Node
{
friend class List;
private:
node_data * nData;
Node *pNext;
Node *pPrev;
public:
Node (node_data * data){nData = data;
pNext = NULL;
pPrev = NULL;}
node_data * getData(){return nData;}
};
这是类列表头文件
class List
{
private:
Node *pHead;
Node *pTail;
Node *createNode(node_data * data);
public:
List ();
~List();
Node *getpHead (){ return pHead;}
Node *getpTail (){return pTail;}
Node *previousNode(Node *pNode){return pNode->pPrev;}
Node *nextNode (Node *pNode){return pNode->pNext;}
void appendNode(node_data * value);
void insertNode(node_data * value, Node *pAfter);
void removeNode(Node *pNode);
bool isEmpty();
void printList();
};
List ::List() {
pHead=NULL;
pTail=NULL;
}
List ::~List(){
while (!isEmpty()) //keep on removing until the
//head points to NULL
removeNode(pHead);
cout << "List deleted\n";
}
Node * List::createNode(node_data * data){
Node * pNode = new Node (data); //allocate memory for new node and
//intialize value to data
return pNode;
}
bool List ::isEmpty(){
return pHead == NULL;
}
void List ::appendNode(node_data * value)
{
Node * pNode = createNode(value);
if (isEmpty()) { //if list is empty
pHead = pNode; //make head point to pNode
pNode->pPrev = NULL;
}
else { //otherwise
pTail->pNext = pNode; //make tail point to pNode
pNode->pPrev = pTail;
}
pTail = pNode; //tail is now pNode
pNode->pNext = NULL; //pNode next now points to NULL
}
void List ::insertNode(node_data * value, Node *pAfter)
{
Node *pNode = createNode(value);
pNode->pNext = pAfter->pNext;
pNode->pPrev = pAfter;
if (pAfter->pNext != NULL)
pAfter->pNext->pPrev = pNode;
else
pTail = pNode;
pAfter->pNext = pNode;
}
void List ::removeNode(Node *pNode)
{
if (pNode->pPrev == NULL) //if removing the head
pHead = pNode->pNext;
else
pNode->pPrev->pNext = pNode->pNext; //if removing a middle node
if (pNode->pNext == NULL) //if removing the tail
pTail = pNode->pPrev;
else
pNode->pNext->pPrev = pNode->pPrev;
pNode = NULL;
delete pNode; //*free the memory
}
void List ::printList()
{
Node *pNode=pHead;
if (isEmpty())
cout << "The list is empty\n";
else
for (pNode = pHead; pNode != NULL; pNode = pNode->pNext)
pNode->nData->print();
}
我的班级签证申请头文件
class visa_application
{
public:
// class constructor
visa_application();
// class destructor
~visa_application();
private:
string visa_type;
int invoice_no;
string surname;
string firstname;
double contact;
string status;
string result;
};
最后是签证申请.cpp
visa_application::visa_application()
{
string visa_type = none;
int invoice_no = 0;
string surname = none;
string firstname = none;
double contact = 00;
string status = none;
string result = none;
}
class destructor
visa_application::~visa_application()
{
// insert your code here
}
在其他一些事情中,我收到错误“没有用于调用 `List::appendNode(Node*&)' 的匹配函数”。无论如何,我知道这很长,但如果我能得到一些帮助,那就太好了,我没有导师或老师或类似的人来帮助我,因此非常感谢任何反馈!谢谢!
**编辑错误消息是:
no matching function for call to `Node::Node()'
candidates are: Node::Node(const Node&)
Node::Node(node_data*)
no matching function for call to `List::appendNode(Node*&)'
candidates are: void List::appendNode(node_data*)