gpt4 book ai didi

c++ - "class has no constructors"类模板错误

转载 作者:太空狗 更新时间:2023-10-29 20:15:03 26 4
gpt4 key购买 nike

我正在制作一个链表,我只想在链表的前面添加一个节点。我做错了什么?

Node.h

 #pragma once

namespace list_1
{
template <typename T>
struct Node
{
T data;
Node<T> *next;

// Constructor
// Postcondition:
Node<T> (T d);
};

template <typename T>
Node<T>::Node(T d)
{
data = d;
next = NULL;
}
}

list.h

template <typename T>
void list<T>::insert_front(const T& entry)
{
Node<T> *temp = head;

if(temp == NULL)
temp->next = new Node(entry);
else
{
while (temp->next != NULL)
{
temp = temp->next;
}

temp->next = new Node(entry);
}
}

错误信息;

 1>------ Build started: Project: Linked List, Configuration: Debug Win32 ------
1> list_test.cpp
1>c:\...\linked list\list.h(54): error C2955: 'list_1::Node' : use of class template requires template argument list
1> c:\...\linked list\node.h(7) : see declaration of 'list_1::Node'
1> c:\...\linked list\list.h(48) : while compiling class template member function 'void list_1::list<T>::insert_front(const T &)'
1> with
1> [
1> T=double
1> ]
1> c:\...\linked list\list_test.cpp(33) : see reference to class template instantiation 'list_1::list<T>' being compiled
1> with
1> [
1> T=double
1> ]
1>c:\...\linked list\list.h(54): error C2514: 'list_1::Node' : class has no constructors
1> c:\...\linked list\node.h(7) : see declaration of 'list_1::Node'
1>c:\...\linked list\list.h(62): error C2514: 'list_1::Node' : class has no constructors
1> c:\...\linked list\node.h(7) : see declaration of 'list_1::Node'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

阅读错误信息:

use of class template requires template argument list

new Node(entry)应该是 new Node<T>(entry)

关于c++ - "class has no constructors"类模板错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14535027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com