Sign in to follow this  
Woblin

Linked List Pointer Problem

Recommended Posts

Hello I have a problem with a linkedlist that I made a while ago and it worked fine, but now the compiler says "cannot convert parameter 1 from 'Mana *' to 'Mana **'" but I didn't change the code and I don't know what the error means. I would appriciate some help, thanks. ************************************************************** main.cpp 1> lltest\linkedlist.h(43) : error C2664: 'Node<T>::Node(T *)' : cannot convert parameter 1 from 'Mana *' to 'Mana **' 1> with 1> [ 1> T=Mana * 1> ] 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 1> lltest\linkedlist.h(42) : while compiling class template member function 'bool LinkedList<T>::addFirst(T)' 1> with 1> [ 1> T=Mana * 1> ] 1> lltest\main.cpp(7) : see reference to class template instantiation 'LinkedList<T>' being compiled 1> with 1> [ 1> T=Mana * 1> ] 1>Mana.cpp ******************************************' And here is the code: Main.cpp
#include "Mana.h"
#include "LinkedList.h"

int main()
{
	LinkedList<Mana*> *mana;
	mana = new LinkedList<Mana*>();

	mana->addFirst( new Mana() );
}


Mana.h
#ifndef MANA_H
#define MANA_H

class Mana{
public:
	Mana()
	{
		nr = 0;
	}
	int nr;
};

#endif

Node.h
#ifndef NODE_H
#define NODE_H

template<class T>
class Node
{
public:
     
     T *data;
     Node<T> *next;
     Node();
     Node(T *theData)
     {
          data = theData;
          next = NULL;
     }
     
     ~Node()
     {
          delete data;
     }
};

#endif

LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include "Node.h"

template<class T>
class LinkedList
{
private:
     int nrOfElements;
     Node<T> *first;
public:
     LinkedList()
     {
          first = NULL;
          nrOfElements = 0;
     }
     
     ~LinkedList()
     {
          while (!isEmpty())
          {
			deleteElementAt(0);
          }
	 }
     
	 int getSize()
     {
          return nrOfElements;
     }
     
     
     bool isEmpty()
     {
          return nrOfElements == 0;
     }
     
     bool LinkedList<T>::addFirst(T data)
     {
          Node<T> *temp = new Node<T>(data);
          
          temp->next = first;
          first = temp;
          
          nrOfElements++;
          return true;
     }
     
     
     bool LinkedList<T>::addElementAt(int index, T data)
     {
          if(index>nrOfElements || isEmpty())
          {
               return false;
          }
          
          Node<T> *temp = new Node<T>(data);
          Node<T> *walker = first;
          if(index==0)
          {
               first = temp;
               temp->next = walker;
          }
          else{
               for(int i=1;i<index;i++){
                    walker = walker->next;
               }
          
          temp->next = walker->next;
          walker->next = temp;     
          }
          
          nrOfElements++;
          
          return true;
     }
     
     bool LinkedList<T>::deleteElementAt(int index)
     {
          if(index > nrOfElements || isEmpty())
          {
               return false;
          }
          
          Node<T> *temp;
          
          if(index==0)
          {
               temp = first;
               first = first->next;
          }
          else 
          {
               Node<T> *walker = first;
               for(int i = 1;i<index;i++)
               {
                    walker = walker->next;
               }
               temp = walker->next;
               walker->next = temp->next;
          }
          
          delete temp;
          nrOfElements--;
          
          return true;
     }
     

     bool LinkedList<T>::deleteElement(T data)
     {
          Node<T> *walker = first;
          Node<T> *temp;
          
          if(first->data == data)
          {
               temp = first;
               first = first->next;
          }
          else
          {
               while(data != walker->next->data && walker->next != NULL)
               {
                    walker = walker->next;
               }
               temp = walker->next;
               walker->next = temp->next;
          }
          
          delete temp;
          nrOfElements--;
          return true;
     }
     

     T LinkedList<T>::getAt(int index)
     {
          if(index>=nrOfElements || isEmpty())
          {
               return NULL;
          }
          
          Node<T> *walker = first;
          
          for(int i=0;i<index;i++)
          {
               walker = walker->next;
          }
          return walker->data;
     }
     

     bool LinkedList<T>::setAt(int index, T data)
     {
          if(index>=nrOfElements || isEmpty())
          {
               return false;
          }
          Node<T> *walker = first;
          for(int i = 0;i<index;i++)
          {
               walker = walker->next;
          }
          walker->data = data;
          return true;
     }
};

#endif


Share this post


Link to post
Share on other sites
Node<SomeType>::data == SomeType*
Node<Sometype*>::data == SomeType**

I strongly suggest using the standard library list (std::list) instead.

Share this post


Link to post
Share on other sites
Well I found one error:


Node(T *theData)
{
data = theData;
next = NULL;
}







bool LinkedList<T>::addFirst(T data)
{
Node<T> *temp = new Node<T>(data);

temp->next = first;
first = temp;

nrOfElements++;
return true;
}





You are passing T to the constructor of Node. The constructor however expects a T*. If T is Node* then you see the error makes perfect sense since T* = Mana** T = Mana*.


Quote:

I strongly suggest using the standard library list (std::list) instead.


QFE. Don't know if you are perhaps only doing this as an exercise, but using std for the end product is a better option than homegrown in almost all cases [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this