C++ Can';我不知道我是不是';我输入的参数不正确或存在';我的密码有问题吗
所以,我才刚刚开始学习链表,我做了一个函数,在链表中插入一个头。我很难弄清楚的问题是我是否输入了错误的参数,或者我的代码是否有一些错误 节点结构:C++ Can';我不知道我是不是';我输入的参数不正确或存在';我的密码有问题吗,c++,C++,所以,我才刚刚开始学习链表,我做了一个函数,在链表中插入一个头。我很难弄清楚的问题是我是否输入了错误的参数,或者我的代码是否有一些错误 节点结构: template<typename T> struct node { public: node(node* next = nullptr, const T& item = T()); template<typename U> friend ostream& operator <&
template<typename T>
struct node
{
public:
node(node* next = nullptr, const T& item = T());
template<typename U>
friend ostream& operator <<(ostream& outs, const node<U> &print_me);
T _item; //crate
node* next; //label
};
模板
结构节点
{
公众:
节点(node*next=nullptr,const T&item=T());
模板
friend ostream&operator您是否在cpp文件中定义了节点
方法?我的节点方法在与节点结构相同的头文件中定义,因为它是模板化的。插入头函数在另一个头文件中定义,我将使用该头文件作为常规链表函数。请创建一个适当的头文件来显示给我们,以便我们确切地知道我需要什么完成了什么以及发生了什么。你的节点
构造函数的实现是什么样子的?在类中你声明了构造函数。然后你也需要定义(实现)它。如果没有定义(实现),那么你会得到一个类似于你的错误。
node<T>* insert_head(node<T> *&head, const T& insert_this)
{
//create new dynamic variable
node<T>* temp = new node<T>; //IT SHOWS THAT THE ERROR OCCURS HERE
//temp takes in the insert_this item
temp->_item = insert_this;
//temp points to the head so it doesn't lose the location
temp->next = head;
//head points back to temp
head = temp;
return head; //returns back the head of the list
}
{
node<int>* head_ptr = nullptr;
for(node<int>* nPtr = head_ptr; nPtr != nullptr; nPtr = nPtr->next)
{
insert_head(head_ptr, 7); //insert integer 5 into the head
PrintList(head_ptr); //print the linked list
}
return 0;
}