I tried your way of doing it salem and I still get a memory error. If you would please look at my entire program to see what I'm doing wrong, I would be very, very grateful.
Code:
#include <iostream>
using std::cout;
using std::endl;
//using std::cin;
class string
{public:
//protected:
struct node
{
node * next;
char c;
node() : next(0) {}
};
unsigned long * len;
node * current;
node * head;
void addnode();
inline void gonext();
inline void gohead();
//public:
string();
virtual ~string();
};
void string::addnode()
{
if(!current->next)
current->next = new node;
else
{
node * newnode = new node;
newnode->next = current->next;
current->next = newnode;
}
}
inline void string::gonext()
{
if(current->next)
current = current->next;
}
inline void string::gohead()
{
current = head;
}
string::string()
{
head = new node;
current = head;
}
string::~string()
{
node * temp;
current = head;
while(current)
{
temp = current->next;
delete current;
current = temp;
cout << "foo" << endl;
}
}
int main()
{
string test;
test.~string();
return 0;
}
i commented out the...what do you call what private/protected/public are?...things...so that I could cout some protected members to see if they are what I expect them to be. They are. My while loop in the destructor seems to run twice and start a third iteration before it crashes when I only have a single node. It crashes when I delete current on the third iteration. Thank you guys for your help so far!
hang on, I've been doing some testing and some weird stuff happens. I used cin at the very end of my main function to stop my program from quitting as soon as it stops executing. My program made it to the cin with no problems. I entered some gibberish to make it continue, and THEN I got my memory error. I have absolutely no clue why this is. Could you please tell me what you think it might be?