0

I can't figure out notation, confused with reference and pointers. I have to put Student pointers into LinkedList. And I think I just put wrong & or *. Here what I do:

int main(){
PointerClass *p = new PointerClass();
Student *s = new Student();
p->add(s);
return 0;
}

On add as I see error:

a reference of type “Student&” (not const-qualified) cannot be initialized

above add is defined as this

void PointerClass::add(Student& a) {
    ll.AddNode(a);
}

where ll is:

private:
    LinkedList ll;

Class LinkedList has following structure:

class LinkedList
{
private:
    typedef struct node {
        Student data;
        node* next;
    }* nodePtr;


    nodePtr head;
    nodePtr curr;
    nodePtr temp;
}

void LinkedList::AddNode(Student& addData) {
    nodePtr n = new node;
    n->next = NULL;
    n->data = addData;

    if (head != NULL) {
        curr = head;
        while (curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = n;
    }
    else {
        head = n;
    }
}
rakamakafo
  • 1,144
  • 5
  • 21
  • 44
  • 1
    Maybe `void PointerClass::add(Student *a) {` and `void LinkedList::AddNode(Student *addData) {` – DimChtz Apr 22 '18 at 19:06
  • 2
    This is rather poor C++ code. Consider switching to a good and modern C++ book as your learning resource, or supplement what ever you are learning from atm with one at least. – Baum mit Augen Apr 22 '18 at 19:07
  • Adding to what @BaummitAugen said, here is a [list of some good books](https://stackoverflow.com/q/388242/9254539). Did nobody ever tell you that `main()` returns an int? – eesiraed Apr 22 '18 at 19:10
  • @FeiXiang that is not main point here. I was trying to grab parts of code into here, so that not to clutter too much, so that is just a trivial typo which doesn't make question wrong at all – rakamakafo Apr 22 '18 at 19:15
  • @DimChtz , thank you . I guess now I understand how stuff works. I did another minor change in struct and that worked as intended – rakamakafo Apr 22 '18 at 19:17
  • @BaummitAugen , thanks for comment, I'm just learning concepts. And if you mention what parts are poor that would be great. PS. I didn't put whole code here, so maybe because i put parts that looks not so great – rakamakafo Apr 22 '18 at 19:24
  • @Sher Littering everything with pointers and having naked `new`s at all are quite an antipattern, for starters. – Baum mit Augen Apr 22 '18 at 19:28
  • @BaummitAugen, maybe. But I have to do this stuff on heap rather than on stack and i also have to practice those pointers. :( – rakamakafo Apr 22 '18 at 19:38

1 Answers1

0

Your functions PointerClass::add() and LinkedList::addNode() are expecting a Student as a parameter, not a Student*. You have two options to make it work:

1) Refactor PointerClass::add() and LinkedList::addNode() to receive and store a Student*. This would basically amount to replacing the &s in the function parameter declarations with *s and either de-referencing that pointer when you put it in your new LinkedList::node, or changing the type of LinkedList::node::data to Student*

2) Pass a Student to PointerClass::add(). This would just be a matter of declaring s with something like Student s = Student();, or de-referencing it before passing it to PointerClass::add().

tdk001
  • 1,014
  • 1
  • 9
  • 16