我在编码链表模板时遇到问题。它可以编译,但我得到以下错误:
1.
1.
1.
1.
1.
任何帮助都将不胜感激,因为在过去的几个小时里,我一直盯着屏幕试图解决这个问题。这是我的密码:
主要的cpp
#include <iostream>
#include <conio.h>
#include "LinkedList.h"
using namespace std;
int main()
{
LinkedList<int> list;
list.insertNode(5);
list.insertNode(7);
list.insertNode(1);
list.insertNode(19);
list.insertNode(16);
list.insertNode(22);
cout << "\nDisplay the list: \n";
list.displayList();
cout << endl;
list.deleteNode(16);
cout << "\nDisplay node with 16 deleted: \n";
list.displayList();
cout << endl;
LinkedList<int> list2 = list;
cout << "\nDisplay List 2: \n";
list2.displayList();
cout << endl;
getch();
return 0;
}
链接列表
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
template <class T>
class LinkedList
{
private:
struct ListTemplate
{
T value;
ListTemplate *next;
};
ListTemplate *head;
public:
//Constructor
LinkedList()
{ head = NULL; }
//Destructor
~LinkedList();
//Copy Constructor
LinkedList(ListTemplate &obj)
{
ListTemplate *nodePtr = &obj; //Move through the list
ListTemplate *newNode = new ListTemplate;
if(!head)
return;
else
{
newNode = head;
while(nodePtr->next)
{
nodePtr = nodePtr->next;
newNode->next = nodePtr;
}
}
}
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList();
};
#endif
链接列表。cpp
#include "LinkedList.h"
#include <iostream>
using namespace std;
template <class T>
void LinkedList<T>::appendNode(T newValue)
{
ListTemplate *newNode; //Point to the new node
ListTemplate *nodePtr; //Move through the list
//Allocate new node and store num there
newNode = new ListTemplate;
newNode->value = num;
newNode->next - NULL;
if(!head)
head = newNode;
else
{
//Initialize the nodePtr as head to move through the list
nodePtr = head;
//While there is still a node, we move through it until there is no more
while (nodePtr->next)
nodePtr = nodePtr->next;
//Append the node
nodePtr->next = newNode;
}
}
template <class T>
void LinkedList<T>::insertNode(T newValue)
{
ListTemplate *newNode; //New Node
ListTemplate *nodePtr; //Move through the list
ListTemplate *previousNode = NULL; //The previous node
//Allocate new node and store num
newNode = new ListTemplate;
newNode->value = num;
//If no node, new node is head
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else //Insert node where it needs to be
{
nodePtr = head; //Beginning position
previousNode = NULL;
//Skip nodes that are less than newNode
while(nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If newNode is the 1st in the list, insert it in slot 1
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else //Insert after previous node
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
template <class T>
void LinkedList<T>::deleteNode(T searchNum)
{
ListTemplate *nodePtr;
ListTemplate *previousNode;
//If the list is empty, do nothing
if(!head)
return;
//See if the first node is the one
if(head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else //Find the node to delete
{
nodePtr = head;
//Skip the nodes not equal to num
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//Delete node and link previous node to the one of the one being deleted
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
template <class T>
LinkedList<T>::~LinkedList()
{
ListTemplate *nodePtr;
ListTemplate *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
template <class T>
void LinkedList<T>::displayList()
{
ListTemplate *nodePtr;
nodePtr = head;
while(nodePtr)
{
cout << nodePtr->value << " ";
nodePtr = nodePtr->next;
}
}
再次感谢你的帮助!
不要将LinkedList分为多个部分。h和。cpp。链接器缺少您放入LinkedList的内容。cpp。
您不应该在源文件中实现模板函数。
请参阅此处-为什么模板只能在头文件中实现?
尝试将所有模板函数移动到头文件(LinkedList. h)中,并重新编译
我一直试图改变输入字段从一个到另一个无济于事,当我使用。焦点它给我一个错误,它不是一个函数。 我将粘贴我的代码 有人能帮忙吗。如果你知道更好的方法,我会很感激你引导我走上正确的道路
函数功能:关闭所有视图 函数方法 closeTextView() 函数用例 showTextView("测试浮动窗口",100,100,600,300,"center","FFFFFF","000000",20,1,1,1,50) mSleep(5000) closeTextView()
函数名称:关闭文件句柄 函数功能:关闭文件句柄 函数方法 io.close() 函数用例 file,msg = io.open("/mnt/sdcard/kazhu.txt") if file then dialog("打开成功",5000) file:close() else dialog("打开失败,失败原因:"..msg,5000) end
我不想更改端口号,但收到异常“exception in thread”main“java.net.BindException:Address already in use:JVM_Bind” ServerSocket服务器=新服务器套接字(1234180);
这个问题似乎与帮助中心定义的范围内的编程无关。 我正在尝试计算软最大激活函数的导数。我发现了这个:https://math.stackexchange.com/questions/945871/derivative-of-softmax-loss-function 似乎没有人给出我们如何得到i=j和i!= j的答案的正确推导。有人可以解释一下吗!当涉及求和时,我对导数感到困惑,就像在softmax
问题内容: 遇到这种奇怪的行为时,我正在玩Javascript创建一个简单的倒计时时钟: 剩下的日子将减少30天。 此代码有什么问题? 编辑:我更改了变量名称,使其更清晰。 问题答案: 对于JavaScript,该月份从零开始。 天和年是基于基础的。