|
-
Nov 13th, 2002, 09:18 PM
#1
Thread Starter
Member
Mystring Class help..
Hey,
I have an assignment for my Data structures class to write my own string class using character arrays with Dynamic Memory Allocation. Its way over due, and I still cant figure out how to do much of anything. Does anyone have a simplified string class where I could understand how to create the class and work on changing it to dynamic memory allocation on my own?
-
Nov 14th, 2002, 09:24 AM
#2
A string class should look something like this:
Code:
class myString
{
private:
char* data;
unsigned int allocated;
public:
//constructor
myString() : data(0), allocated(0) {}
myString(const myString& that)
{ /* Allocate memory, copy data from 'that' */ }
//destructor
~mystring()
{ /* Free the memory allocated for 'data' */ }
//operators
myString& operator = (const myString& that)
{
// Ensure that 'data' is large enough, if not, free the current memory, and allocate more memory
// Copy
}
// More functions:
// operator += for appending strings
// c_str() that returns 'data' with a terminating 0
// all of the operators again that take a char* instead of a myString
};
-
Nov 14th, 2002, 11:06 AM
#3
Thread Starter
Member
thanks
Thanks alot.. I should be able to figure it out a little easier now.. I hope..
-Jason
-
Nov 14th, 2002, 04:16 PM
#4
Thread Starter
Member
Okay so i'm working with the mystring class. Currently i am trying to overload the extraction operator. I came up with the code below, which I know wont show me the entire string but should display the first character of the string (i'll figure out how to actually do that later). But when I go to compile it with this I get these two error messages.
"Error E2080 mystring.cpp 28: 'mystring: perator <<(ostream &,const mystring &)' must be declared with one parameter
Error E2280 mystring.cpp 52: Member identifier expected in function operator <<(ostream &,const mystring &)"
//Class member description
ostream& operator <<(ostream&, const mystring&);
//My implementation of the extraction operator
ostream& mystring: perator <<(ostream& out, const mystring& tmp)
{
out<< tmp.(*theBuffer);
return out;
}
If anyone could explain to me what i'm doing wrong I would really appreciate it. I dont usually have this much trouble with classes, but it all doesnt come easy to me yet.
-
Nov 14th, 2002, 05:06 PM
#5
operator << shouldn't be a member of the string class, if it is a member it has 3 parameters (the two you see, and the this pointer)
-
Nov 15th, 2002, 07:25 AM
#6
It can't be. If it was it would have only one parameter, but it wouldn't work the way it should.
It has to be a global function.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|