Hi, I'm fairly new to C++ and wanted to know if you all could help me with something here. I have this bit of code that I'm certain works but there is a problem with where I put the variables or what not. I'm simply trying to make a linked list and so far this is what I have but it is giving me error LNK2019 unresolved external symbol.
Here is the code that i have so far. Thank you all for your time.
For the source
#include <iostream>
usingnamespace std;
class stackLL
{
private:
class node
{
public:
int data; // for holding data in the node
node *next; // for pointing to the next.
};
node *top;
public:
stackLL();
~stackLL();
//return true if empty, false if not
bool empty()
{}
//add item to top of stack
void push(int x)
{}
//remove and return top item from stack
int pop()
{}
//additional "weird" methods: TBA
};
stackLL::stackLL()
{
top = NULL;
}
Hmm what compiler are you using? I didn't get that error when compiling with g++ but it gives an error about undefined reference to `stackLL::~stackLL()'. if you simply add the basic outline for the destructor the program compiles with no errors.
also as far as it working you would have to implement the unimplemented functions for this to really work. right now it wont actually create a linked list since push pop and empty don't do anything.
I know what you mean since so far its a work in progress. I'm using visual studio 2012 ultimate. I wanted to at least make sure that it is working before I continue cause I wanted to make sure I can fix the errors one at a time other than writing everything and fixing alot of errors.
You know that is one thing I never really truly understood. I needed to know what everyone is talking about. They say you need a deconstructor to prevent memory leaks and such and I always wondered, what exactly are they talking about? And another question about this is, if I were to make a constructor for ~stackLL like this stackLL::~stackLL() then I should clear it up?
should clear your error but you still need to add functionality to it. As far as memory leaks you can find more info on them at http://en.wikipedia.org/wiki/Memory_leak
here's an example on how you might implement it
1 2 3 4 5 6 7 8 9 10 11 12
stackLL::~stackLL()
{
node* nextNode;
node* current = top;
while(!empty())
{
nextNode = current->next;
delete current;
current = nextNode;
}
top = NULL;
}
a memory 'leak' is kind of a misnomer, memory isnt leaking, its just that if you initialize an object, and then stop using that object but dont destruct or free up the memory it resided in, then you lose that chunk of memory. Using a destructor frees up the memory space the object took up. It may seem harmless and with small test functions it kind of is, but get a big app with lots of objects being initialized and then discarded but not properly cleaned up, and you run out of memory fast, like its leaking out of your system and you run the risk of it drying up. :)
you can also have a memory leak by losing a reference to the data. ie you have a node of data but dont have the pointer to it so there is data but no way to access it.
@K0T4K0t4
I just tried initializing ~stackLL and all I added was stackLL::~stackLL() {} and it stopped complaining about the error.
@gtm I see what you mean about that but the thing I wonder for the ~stackLL space I have, do I have to put anything in there for me to get it to free up the memory or do I just have to have a class constructor for it to get it to free up the memory?
Cool cause as you can see I have to implement it in three ways. Stack, last in first out, queue, first in first out, and priority but I'm not so sure how to do that. At least not yet.
constructor/destructor are sometimes written as ctor/dtor.
Constructors are called when you create an object (instance of a class) on the stack or on the free store (heap). Which ctor depends on the arguments in the call. Learn what default ctor and copy ctor are and how arguments are used to overload ctors and rules for matching arguments to parameter types.
Destructors are called when an object goes out of scope if it is on the stack (local variable) or is explicitly deleted from the free store or heap. There can be only one dtor. When inheritence is used, learn order of dtor calls.
#include <iostream>
usingnamespace std;
class stackLL
{
private:
class node
{
public:
int data; // for holding data in the node
node *next; // for pointing to the next.
};
node *top;
public:
stackLL();
~stackLL();
//return true if empty, false if not
bool empty()
{}
//add item to top of stack
void push(int x)
{
node *tmp;
tmp = new node;
cout<<top << endl;
data = x;
cout<<data<<endl;
}
//remove and return top item from stack
int pop()
{}
//additional "weird" methods: TBA
};
stackLL::stackLL()
{
top = NULL;
}
stackLL::~stackLL()
{
}
Cool, I have actually made progress since then but now I have a new problem. For some reason, my code gets stuck in an infinite loop here on pop which I temp made as a transverse function to ensure my stack is working correctly. Here is what I have.
#include <iostream>
usingnamespace std;
class stackLL
{
private:
class node
{
public:
int data; // for holding data in the node
node *next; // for pointing to the next.
};
node *top;
node *nxt;
public:
stackLL();
~stackLL();
//return true if empty, false if not
bool empty()
{}
//add item to top of stack
void push(int x)
{
node *tmp;
tmp = new node;
if (top == NULL)
{
tmp->data = x;
top = tmp;
tmp->next = NULL;
}
tmp->data = x;
tmp->next = top;
top = tmp;
}
//remove and return top item from stack
int pop()
{
nxt = top;
while (nxt != NULL)
{
cout<<nxt->data<<endl;
nxt = nxt->next;
}
return 0;
}
};
stackLL::stackLL()
{
top = NULL;
}
stackLL::~stackLL()
{
}