链接列表(从 'Node<int>*' 到 'int' [-fpermissive]| 的无效转换)
Linked list (invalid conversion from 'Node<int>*' to 'int' [-fpermissive]|)
我想在 C++ 中创建类似于 Arraylist 的节点。当我创建一个方法 get();它说了错误。不懂就去网上找答案。你能帮我找到这个答案吗?
template<typename T>
struct Node{ //Node
T data;
Node<T> *next;
Node(){
next=NULL;
}
Node(T value){
data=value;
next=NULL;
}
};
template<typename T>
class LinkedList{ //class Node
public:
Node<T> *head;
Node<T> *tail;
void add(T value){ //method create newnode add to tail
Node<T> *newNode=new Node<T>(value);
if (head == NULL){
head=newNode;
tail=newNode;
}
else {
tail->next=newNode;
tail=newNode;
}
}
void PrintAll(string Name){ //method print all node
Node<T> *current;
int i=0;
current=head;
while (current != NULL ){
printf("%s[%d]=%d\n",Name.c_str(),i,current->data);
current=current->next;
i++;
}
}
T get(int index){ //method show node in parameter
Node <T> *current=head;
int count=0;
while (current != NULL){
if ( count == index) return current->next;
current=current->next;
count++;
}
}
};
错误:从 'Node*' 到 'int' 的无效转换 [-fpermissive]|
警告:控制到达非空函数的结尾 [-Wreturn-type]|
在 get()
内,您返回的是 Node*
而不是 T
,准确地说是在 if
内。
你应该这样做:
T get(int index){ //method show node in parameter
Node <T> *current=head;
int count=0;
while (current != NULL){
if ( count == index) return current->data;
current=current->next;
count++;
}
}
你还应该处理索引无效的情况,在这些情况下抛出异常是可以的。
结构 Node
应该在 class LinkedList
中定义并且是 class.
的私有成员
成员函数PrintAll
应该用
限定符 const
(函数名应以小写字母开头)。
函数get
将在列表包含的数据少于指定索引的情况下抛出异常。否则它应 return 找到的节点的数据成员 data
的值。
这是一个演示程序,展示了如何定义列表。
#include <iostream>
#include <string>
#include <stdexcept>
template<typename T>
class LinkedList
{
private:
struct Node
{
T data;
Node *next;
Node() : data( T() ), next( nullptr )
{
}
Node( const T &value ) : data( value ), next( nullptr )
{
}
};
Node *head = nullptr;
Node *tail = nullptr;
public:
void add( const T &value )
{
Node *newNode = new Node( value );
if ( head == nullptr )
{
head = tail = newNode;
}
else
{
tail = tail->next = newNode;
}
}
void printAll( const std::string &name = "" ) const
{
size_t i = 0;
for ( auto current = head; current != nullptr; current = current->next )
{
std::cout << name << i++ << ": " << current->data << '\n';
}
}
T get( size_t index ) const noexcept( false )
{
auto current = head;
while ( index != 0 && current != nullptr )
{
current = current->next;
--index;
}
if ( current == nullptr ) throw std::out_of_range( "Too big index" );
else return current->data;
}
};
int main()
{
LinkedList<int> list;
list.add( 1 );
list.add( 2 );
list.add( 3 );
list.printAll();
try
{
for ( size_t pos = 0; ; ++pos )
{
auto value = list.get( pos );
std::cout << "At position " << pos << " there is stored " << value << '\n';
}
}
catch ( const std::exception &ex )
{
std::cout << ex.what() << '\n';
}
}
程序输出为
0: 1
1: 2
2: 3
At position 0 there is stored 1
At position 1 there is stored 2
At position 2 there is stored 3
Too big index
当然,您应该在列表的定义后附加析构函数、复制构造函数和复制赋值运算符。在这种情况下,您还必须明确定义默认构造函数。
我想在 C++ 中创建类似于 Arraylist 的节点。当我创建一个方法 get();它说了错误。不懂就去网上找答案。你能帮我找到这个答案吗?
template<typename T>
struct Node{ //Node
T data;
Node<T> *next;
Node(){
next=NULL;
}
Node(T value){
data=value;
next=NULL;
}
};
template<typename T>
class LinkedList{ //class Node
public:
Node<T> *head;
Node<T> *tail;
void add(T value){ //method create newnode add to tail
Node<T> *newNode=new Node<T>(value);
if (head == NULL){
head=newNode;
tail=newNode;
}
else {
tail->next=newNode;
tail=newNode;
}
}
void PrintAll(string Name){ //method print all node
Node<T> *current;
int i=0;
current=head;
while (current != NULL ){
printf("%s[%d]=%d\n",Name.c_str(),i,current->data);
current=current->next;
i++;
}
}
T get(int index){ //method show node in parameter
Node <T> *current=head;
int count=0;
while (current != NULL){
if ( count == index) return current->next;
current=current->next;
count++;
}
}
};
错误:从 'Node*' 到 'int' 的无效转换 [-fpermissive]| 警告:控制到达非空函数的结尾 [-Wreturn-type]|
在 get()
内,您返回的是 Node*
而不是 T
,准确地说是在 if
内。
你应该这样做:
T get(int index){ //method show node in parameter
Node <T> *current=head;
int count=0;
while (current != NULL){
if ( count == index) return current->data;
current=current->next;
count++;
}
}
你还应该处理索引无效的情况,在这些情况下抛出异常是可以的。
结构 Node
应该在 class LinkedList
中定义并且是 class.
成员函数PrintAll
应该用
限定符 const
(函数名应以小写字母开头)。
函数get
将在列表包含的数据少于指定索引的情况下抛出异常。否则它应 return 找到的节点的数据成员 data
的值。
这是一个演示程序,展示了如何定义列表。
#include <iostream>
#include <string>
#include <stdexcept>
template<typename T>
class LinkedList
{
private:
struct Node
{
T data;
Node *next;
Node() : data( T() ), next( nullptr )
{
}
Node( const T &value ) : data( value ), next( nullptr )
{
}
};
Node *head = nullptr;
Node *tail = nullptr;
public:
void add( const T &value )
{
Node *newNode = new Node( value );
if ( head == nullptr )
{
head = tail = newNode;
}
else
{
tail = tail->next = newNode;
}
}
void printAll( const std::string &name = "" ) const
{
size_t i = 0;
for ( auto current = head; current != nullptr; current = current->next )
{
std::cout << name << i++ << ": " << current->data << '\n';
}
}
T get( size_t index ) const noexcept( false )
{
auto current = head;
while ( index != 0 && current != nullptr )
{
current = current->next;
--index;
}
if ( current == nullptr ) throw std::out_of_range( "Too big index" );
else return current->data;
}
};
int main()
{
LinkedList<int> list;
list.add( 1 );
list.add( 2 );
list.add( 3 );
list.printAll();
try
{
for ( size_t pos = 0; ; ++pos )
{
auto value = list.get( pos );
std::cout << "At position " << pos << " there is stored " << value << '\n';
}
}
catch ( const std::exception &ex )
{
std::cout << ex.what() << '\n';
}
}
程序输出为
0: 1
1: 2
2: 3
At position 0 there is stored 1
At position 1 there is stored 2
At position 2 there is stored 3
Too big index
当然,您应该在列表的定义后附加析构函数、复制构造函数和复制赋值运算符。在这种情况下,您还必须明确定义默认构造函数。