-3

I'm trying to add a node onto the beginning of a linked list (push function). I'm getting 2 errors:

1) invalid conversion from 'Node int*' to 'int' (points to test.Push(&test2); in main())

2) initializing argument 1 of 'int Linked_List::Push(ItemType) [with ItemType = int]' (points to function push)

I'm really not sure what the problem is. If I remove the & out of test.Push(&test2); in main() then I get a lot more errors, so I assume it's correct.

//.h
#ifndef Linked_List_h
#define Linked_List_h

template <typename ItemType>
class Node
{
    public:
        ItemType Data;
        Node <ItemType> *next;
};

template <typename ItemType>
class Linked_List
{
        public:
        Node <ItemType> *start;
        Linked_List();
        int Push(ItemType newitem);
};
#endif

.

//.cpp
#include "Linked_List.h"

template <typename ItemType>
Linked_List <ItemType>::Linked_List(){
    start = NULL;
}

template <typename ItemType>
int Linked_List <ItemType>::Push(const ItemType newitem){    //error
    Node <ItemType> *nnode;  //create new node to store new item
    nnode -> next = start -> next;  //new item now points previous first item of list
    start -> next = nnode;   //'start' pointer now points to the new first item of list
    return 1;
}

int main(){
    Linked_List <int> test;
    Node <int> test2;
    test2.Data = 4;
    test.Push(&test2);  //error
}
Seb
  • 1,966
  • 2
  • 17
  • 32
  • first read [this](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) please. – user1810087 May 08 '14 at 15:55
  • 1
    @Foxic - Your `Linked_List` class uses `int` as the template parameter. Why are you pushing on `Node` pointers? Why is main() even getting involved in using `Node`s at all? Isn't that the responsibility of the Linked_List class to figure out how to create and maintain `Node`s? – PaulMcKenzie May 08 '14 at 15:59
  • "If I remove the & .. then I get a lot more errors, so I assume it's correct." that's not necessarily true. You are effectively [Programming by Coincidence](http://pragprog.com/the-pragmatic-programmer/extracts/coincidence). Please read up on C++. – lethal-guitar May 08 '14 at 16:00
  • Thanks I got that part to work. Now can someone tell me why **Node *nnode = new node;** isn't working? Says error: expected type-specifier before 'node'. Is there another way I can do that? – Seb May 08 '14 at 16:18

2 Answers2

1

Your function's signature expects a ItemType, which is int in your case:

int Push(ItemType newitem);

But you are trying to pass a Node<ItemType>, hence you get an error.

Your Push function is already creating a a node internally, so you'd pass the integer directly into it:

Linked_List <int> test;
int test2 = 4;
test.Push(test2);

I need to point out that your code has several other problems besides that, though - for starters, this snippet:

Node <ItemType> *nnode;  //create new node to store new item

Does not create a Node - it just declares a pointer.

I'd strongly advise you to read up on C++'s basics.

lethal-guitar
  • 4,438
  • 1
  • 20
  • 40
  • I guess I commented wrong, it should still work as a linked list though right? – Seb May 08 '14 at 16:03
  • @Foxic well, it's the responsibility of `Push` and the other functions in your list class to ensure that. A user of your class shouldn't even need to know that there are "nodes" involved. – lethal-guitar May 08 '14 at 16:05
1

Push takes the template type, so int in this case. What you want to do is something like:

Linked_List<int> test;
test.push(4);
Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94
  • Now my program just shuts down, "*.exe has stopped working", not sure how to debug so idk if you can help with that – Seb May 08 '14 at 16:03
  • @Foxic feel free to ask a new question, and mark one of the answers here as accepted if it solved your current problem (compiler error). – lethal-guitar May 08 '14 at 16:04
  • 1
    @Foxic - If you write programs like this, you must know how to debug them (not may know, not probably know, but *must* know). Now is the time to learn how to use your compiler's debugger. So what compiler are you using? – PaulMcKenzie May 08 '14 at 16:05
  • Mingw. The problem is with the lines that include a '->' in Push. I want to add **node = new node** in hopes it'll fix it but it just says **"expected type-specifier before 'node'"** and **"expected ; before 'node'"** – Seb May 08 '14 at 16:12
  • 1
    @Foxic - `I want to add node = new node in hopes it'll fix it` Sounds like you're just throwing stuff at the wall and hoping something sticks. You can't learn programming, and especially C++ programming, in that fashion. – PaulMcKenzie May 08 '14 at 16:18
  • My professor does it that way, he showed us how to implement a stack and used that exact line, I'm basically copying him line-for-line. Either way, I try to see what works and what doesn't because I'm really stumbled here. – Seb May 08 '14 at 16:21
  • Edit: I was right, it did fix all my problems. I don't think I was just throwing stuff at the wall, I couldn't just create a lone pointer and adding 'new' helped – Seb May 08 '14 at 16:54
  • @Foxic don't forget to delete the pointer somewhere.. – lethal-guitar May 08 '14 at 16:57