When you are going to use functions from a header, you have to include it (obviously).
It doesn't count if another header (like <iostream>) happens to include <cstdlib> by chance. That's an implementation detail and it is nothing you are allowed to rely on. It might just as well change in the next update to your standard library implementation.
That will make your program to compile.
Also:
If you allocate with new then deallocate with delete
If you allocate with new [] then deallocate with delete []
well aren't there any user defined classes for a string on cplusplus.com. Its a fairly straightforward topic, or so I would think... i'm surprised I haven't been able to google more of it on this site.
How do you intend to instantiate this class? (what is thing expected to be?) In the node class zero is assigned to it. Also, I think you want delete, not delete[], in there.
What is this concept of a node in a string class, anyway? This looks like a messy merge of a linked list and a string class all in one. Google for examples of string classes [again] (there are plenty of examples out there).
well i just threw it together in a hurry, and yeah i guess it is sorta a linked list, but whats the difference right?
And doesn't delete[] just automatically free up any and all memory that had been allocated for a specific variable?
And thing is just supposed to be anything, probably will be instantiating it 99% of the time with <char> though. But i just thought that it might be useful if someday I wanted to have a string of ints for example, i wouldn't have to bother to remember the code for getting the # of digits in an int, i could simply just call the .size() function.
Include all that you need. (string)
Don't use namespace in headers. It pollutes whatever it's next.
AFAIK template and inline functions definitions must be in headers. Others mustn't (linker multiple definition)
Check out for memory leaks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
mystring<something_big>();
mystring()
{
it = new thing;
it[0] = 0; //quite restrictive
nul = true;
//...
~mystring()
{
--num_of_this_active;
--total_mystring_active;
delete [] caddress;
if (nul)
return; //'it' was not released
¿why are you mixing new and new[]?
char * mystring<thing>:: c_str(char *& c) //c is not used in the method
1 2 3
if (size() == 0)//...
if (size() == 1)//...
if (size() > 1)//...
hmmm not sure, think I planned on using caddress when calling c.str(), which I guess I would have to make it public then....
and 10 is meaningless but +48 is for ints. I wanted one single class that I could use for strings and ints and call the same function size() to get meaningful results. .size() for a char * returns the number of characters, .size() for an int returns the number of digits.
Plus it really sucks when writing a templated function only to find it don't work for some classes because some classes might not have a certain function with the same name such as size() or c_str() or etc you get the idea.