Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的模板链表不起作用?_C++_Templates_Linked List - Fatal编程技术网

C++ 为什么我的模板链表不起作用?

C++ 为什么我的模板链表不起作用?,c++,templates,linked-list,C++,Templates,Linked List,我无法说明问题是什么,也没有任何错误。这是我的代码: 节点h: #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; template <typename T> class LinkedList; template <typename T> class Node { public: Node(T data) : data(data),previousNode(null

我无法说明问题是什么,也没有任何错误。这是我的代码:

节点h:

#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;

template <typename T>
class LinkedList;

template <typename T>
class Node {

public:
Node(T data) : data(data),previousNode(nullptr),nextNode(nullptr) {}

Node<T>* GetNextNode() const {
    return nextNode;
}

void SetNextNode(Node<T>* nextNode) {
    this->nextNode = nextNode;
}

Node<T>* GetPreviousNode() const {
    return previousNode;
}

void SetPreviousNode(Node<T>* previousNode) {
    this->previousNode = previousNode;
}

T GetData() const {
    return data;
}

void SetData(T data) {
    this->data = data;
}

private:
T data;
Node<T>* previousNode;
Node<T>* nextNode;
};

#endif  /* NODE_H */
Booking.ccp:

#include "booking.h"

Booking::Booking(long bookingID):bookingID(bookingID){}

long Booking::getBookingID(){
   return bookingID;
}
main.cpp:

#include <QCoreApplication>
#include <cstdlib>
#include <linkedList.h>
#include <iostream>
#include <string>
#include <booking.h>
using namespace std;

int main(int argc, char *argv[]){


  QCoreApplication a(argc, argv);

  LinkedList<Booking*> allBookings;
  Booking* booking1 = new Booking(12);
  allBookings.insertNode(booking1);
  long test =  allBookings.getNode()->getBookingID();

  cout << "test: " << test << endl; //doesn't print anything


  return a.exec();
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
qcorea应用程序(argc、argv);
LinkedList所有预订;
预订*预订1=新预订(12);
allBookings.insertNode(booking1);
长测试=allBookings.getNode()->getBookingID();

insertNode

if (root == nullptr) {
    root = node;
    node->SetNextNode(end);
    end->SetPreviousNode(node);
}else{
当您到达
end->SetPreviousNode(节点);
时,
end
的值为
nullptr
,这可能会使程序崩溃。(您应该能够在调试器中确认)


我想提出一个解决方案,但我不清楚您想做什么。代码似乎有点复杂。另外,我不喜欢在列表中包含光标的设计决策。如果您能够的话,我会删除它。

end->SetPreviousNode(节点)
end==nullptr
将导致分段错误。无关:永远不要在头文件的全局命名空间中使用
名称空间std;
。感谢您向我指出这一点。我想我已经修复了它!
#include <QCoreApplication>
#include <cstdlib>
#include <linkedList.h>
#include <iostream>
#include <string>
#include <booking.h>
using namespace std;

int main(int argc, char *argv[]){


  QCoreApplication a(argc, argv);

  LinkedList<Booking*> allBookings;
  Booking* booking1 = new Booking(12);
  allBookings.insertNode(booking1);
  long test =  allBookings.getNode()->getBookingID();

  cout << "test: " << test << endl; //doesn't print anything


  return a.exec();
}
if (root == nullptr) {
    root = node;
    node->SetNextNode(end);
    end->SetPreviousNode(node);
}else{