链接列表(从'Node <int> *'到'int'[-fpermissive] |的无效转换)

时间:2019-07-09 13:31:19

标签: c++ list templates exception singly-linked-list

我想在c ++中创建Node likes 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] |

的结尾

2 个答案:

答案 0 :(得分:2)

准确地说,在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++;
        }
    }

您还应该处理索引无效的情况,在这种情况下可以抛出异常。

答案 1 :(得分:0)

结构Node应该在类LinkedList中定义,并且是该类的私有成员。

成员函数PrintAll应该用 限定符const(并且函数名称应以小写字母开头)。

如果列表包含的数据少于指定的索引,则函数get将引发异常。否则,它将返回找到的节点的数据成员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

当然,您应该在列表的定义后附加析构函数,复制构造函数和复制赋值运算符。在这种情况下,您还必须显式定义默认的构造函数。