-
error with strings...
I'm am working on a string class. Each character of the string occupies one node. The function that my string class uses to convert from char * to string (the name of my string class) is giving me problems. Because my code is fairly short still, I will post all of it:
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
class string
{
protected:
struct node
{
node * next;
char c;
node() : next(0) {}
};
node * head;
node * current;
public:
string();
string(char *);
string & operator=(string &);
};
string::string()
{
head = new node;
current = head;
}
string::string(char * str)
{
current = head;
unsigned int a = 0;
while(str[a] != '\0')
{
if (current == 0)
current = new node;
/*the problem*/current->c = str[a];
cout << current->c << endl;
a++;
current = current->next;
}
}
string & string::operator=(string & s)
{
s.current = s.head;
current = head;
while (s.current != 0)
{
if (current == 0)
current = new node;
current->c = s.current->c;
s.current = s.current->next;
current = current->next;
}
if (current->next != 0)
{
current = current->next;
while (current != 0)
{
delete current;
current = current->next;
}
}
}
int main()
{
string test = "fart";
return 0;
}
The problem occurs on, guess where, the line that has the comment "the problem". I have no idea why I get an unhandled exception message because of this line. Any help anyone can give would be very much appreciated.
-
what is c? I'm assuming a string.
is it in the same class?
edit: why are you defining current twice?
can you post your class interface?
edit2: I see you edited it as I posted mine :)
-
sorry, I clicked submit by mistake and my incomplete message was posted.
-
>Each character of the string occupies one node.
That's a bit wasteful, don't you think?
>I have no idea why I get an unhandled exception message.
How about some more code. For example, something we can compile to get the error. :) I would wager that you only create a new node when current is null, but since you never set current->next to be null you access an uninitialized pointer.
[edit]
Your linked list code is flawed. Stripping out the unnecessaries, this would be a good start:
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
class string
{
protected:
struct node
{
node * next;
char c;
node() : next(0) {}
};
node * head;
node * current;
public:
string();
string(char *);
};
string::string()
{
head = new node;
current = head;
}
string::string(char * str)
{
current = head = 0;
unsigned int a = 0;
while(str[a] != '\0')
{
if (current == 0) {
current = new node;
current->next = 0;
}
current->c = str[a];
cout << current->c << endl;
a++;
current = current->next;
}
}
int main()
{
string test = "fart";
return 0;
}
[/edit]
-
well I compiled your code, and that particular line didn't give me any errors. The error exists here:
Code:
string & string::operator=(string & s)
{
s.current = s.head;
current = head;
while (s.current != 0)
{
if (current == 0)
current = new node;
current->c = s.current->c;
s.current = s.current->next;
current = current->next;
}
if (current->next != 0)
{
current = current->next;
while (current != 0)
{
delete current;
current = current->next;
}
}
}
and this function has to return a value, and it doesn't
-
I do feel stuipid. Thank you very much for helping me, it would have taken me a long time to figure the out the error with not initializing head and current. I probably would have figured out the deal without returning anything in my operator=() function because I do that all the time. Thanks to all for you help!
prelude - what else do you suggest other than making each character have it's own node?
-
>what else do you suggest other than making each character have it's own node?
A dynamic array. That way each character doesn't have hidden overhead, and you can use standard string handling functions instead of having to roll your own specialized list processing routines. As it is, having a single character plus a pointer (which is usually the equivalent of 2 or 4 characters in size), increases the memory requirements of your strings considerably.
-
could you give me an example of a dynamic array please?
-
>could you give me an example of a dynamic array please?
Code:
char *str = new char[50];
Now you have a dynamic array of 50 characters to work with. Don't forget to delete [] str; when you're done with it.
-
Ok, thanks a lot for all of your help!