Dominique
Dominique

Reputation: 17493

Compilation issue while creating object oriented linked list (compiler error C2664)

I'm writing a class, embedding a reversible linked list, and I'm having trouble with the type of the null pointer:

Linked list definition:

struct LL {
  int information;
  LL* pre;
  LL* succ;
  };

Class definition (partially):

class T_LL {
  private : 
    LL  *lList;
    int index;
    int size;
  public :
    T_LL() {
      lList = new(LL);
      lList->pre = nullptr;
      lList->succ = nullptr;
      size = 0;
      index = -1;
    }
  ...
    LL get_successor(){
      if (index+1 <= size) {
        return *lList->succ;
      } else { return nullptr; }
    }

When I try to compile this, the compiler complains about the get_successor() method, saying:

error C2664: 'LL::LL(const LL &)' : cannot convert argument 1 from 'nullptr' to 'const LL &'

I thought that the nullpointer was a general pointer, which can be used for any purposes? What am I doing wrong (and why is there no compilation error in the constructor)?

Thanks in advance

Upvotes: 0

Views: 55

Answers (1)

John Zwinck
John Zwinck

Reputation: 249123

get_successor() returns a value of type LL. It cannot return a pointer. You may want to change it:

LL* get_successor(){
  if (index+1 <= size) {
    return lList->succ;
  } else { return nullptr; }
}

Upvotes: 1

Related Questions