Invalid conversion from List* to int

i created a linked list : struct Node and List Class and i used it outside with my main method,

#include "Lists.cpp"
#include <cstdlib>
using namespace std;

int main(){


    Lists l = new Lists(1);

    l.add(2);
    l.add(3);
    l.add(4);
    l.add(5);



    system("pause");
    return 0;
        }

but it produces an error that says "invalid conversion from List* to int". Is my using of outside class right? Im a little confused how I will solve this.

#include <cstdlib>
#include <iostream>

using namespace std;

struct Node{
       int data;
       Node *next;       
       Node(int i){
                data = i;
                next = NULL;
                }

       };

class List{
      Node *head;



      public:
      List(int i){
               head = new Node(i);
      }

      void addToHead(int i){
          Node *temp = new Node(i);
          temp->next = head;
          head = temp;
           }

      void add(int i){
                Node *currNode = head;
                while(currNode!= NULL){
                        if(currNode->next == NULL){
                          currNode->next = new Node(i);
                          break;         
                                }
                         else{
                          currNode = currNode-> next;      
                              }   
                                 }

                }
      void deleteNode(int i){
           Node *currNode = head;
           Node *prevNode = NULL;

           while(currNode!=  NULL){
                             if(currNode->data == i) {
                             if(prevNode== NULL){
                                           head = head->next;
                                           }                  
                              else{
                                   prevNode->next = currNode->next;
                                   }                                
                              }
                              prevNode = currNode;
                              currNode =  currNode -> next;                            
                             }
           }
      void insert(int position, Node *n){
           Node *currNode= head;
           Node *prevNode = NULL;

           for(int counter = 0; counter>= position && currNode!= NULL; counter++){
                   if(counter==position){
                              Node *temp = currNode;
                              n->next  =  currNode;
                              prevNode->next= n;                              
                              }                  
                   prevNode = currNode;
                   currNode = currNode-> next;
                   }
           }
      void traverse(Node *node){
           if(node!=NULL){
           cout<<  node-> data  <<endl;
           traverse(node->next);
                          }
           }

      };

Answers


Lists l = new Lists(1);

should be:

Lists *l = new Lists(1);

new provides a pointer.

The reason you get that specific error is that the line would be valid if the conversion chain Lists * -> int -> Lists were valid. The second is valid here because of the constructor but the first is not.


Need Your Help

Understanding Mounted Directories with Fig and Docker

docker fig

I am confused about fig and docker containers.

Connecting mock and client in django's tests

django testing mocking

I've written two classes: System and Order which represent something like "online ticket distribution store".

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.