I need Help please

S

Slugger819

I'm creating a class SString , it should be just string

here what I have for the private data member

int count_;
int capacity_=0;
char* str = new char[capacity_];

and here is my constructor :

SString::SString (int capacity=50):capacity_()

{
capacity_=capacity;

}

but for some reason I'm getting a lots of errors
I have all the #includes statements and all the ";" please help me and
lemme know what I'm doing wrong
 
K

Karsten Baumgarten

Slugger819 said:
I'm creating a class SString , it should be just string

here what I have for the private data member

int count_;
int capacity_=0;
char* str = new char[capacity_];

You cannot initialize class members that way. Do that in the constructor.
and here is my constructor :

SString::SString (int capacity=50):capacity_()

Initialization list must look like this: capacity_ (capacity)
{
capacity_=capacity;

}

Either you use the initialization list or assign the value in the ctor
body (which is less efficient, but it works).

Final question (should have been the first and only actually): Why are
you trying to reinvent the wheel. Use the existing STL string class.

Regards,

Karsten
 
C

Clark S. Cox III

I'm creating a class SString , it should be just string
here what I have for the private data member

Assuming that you're doing this as a learning experience (otherwise,
just use std::string)
int count_;
int capacity_=0;
char* str = new char[capacity_];

You cannot initialize members like this. Remember that the members
don't actually exist outside of instances of your class.
and here is my constructor :

SString::SString (int capacity=50):capacity_()

{
capacity_=capacity;
}

Try this:

SString::SString(int capacity=50)
: count_(0), capacity_(capacity), str(new char[capacity_])
{

}
but for some reason I'm getting a lots of errors I have all the
#includes statements and all the ";" please help me and
lemme know what I'm doing wrong

Even if you still don't want to use std::string, might I suggest using
std::vector<char> for your internal storage, instead of a raw character
array.
 
S

Slugger819

Thanks a lot guys , I know there is a string class , but I'm trying to
create my own

Abe
 
T

Thomas Matthews

Slugger819 said:
Thanks a lot guys , I know there is a string class , but I'm trying to
create my own

Abe

Do you realize there are a lot of string classes out there
besides the STL version?

I suggest reviewing the other libraries out there and only
wasting your time creating a new one if and only if you
can't find one to suit your needs.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,649
Latest member
MargaretCo

Latest Threads

Top