Connecting Tech Pros Worldwide Forums | Help | Site Map

I need Help please

Slugger819
Guest
 
Posts: n/a
#1: Jul 23 '05
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







Karsten Baumgarten
Guest
 
Posts: n/a
#2: Jul 23 '05

re: I need Help please


Slugger819 wrote:[color=blue]
> 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_];[/color]

You cannot initialize class members that way. Do that in the constructor.
[color=blue]
>
> and here is my constructor :
>
> SString::SString (int capacity=50):capacity_()[/color]

Initialization list must look like this: capacity_ (capacity)
[color=blue]
> {
> capacity_=capacity;
>
> }[/color]

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
Clark S. Cox III
Guest
 
Posts: n/a
#3: Jul 23 '05

re: I need Help please


On 2005-02-05 22:59:08 -0500, "Slugger819" <abe819@gmail.com> said:
[color=blue]
> I'm creating a class SString , it should be just string
> here what I have for the private data member[/color]

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

You cannot initialize members like this. Remember that the members
don't actually exist outside of instances of your class.
[color=blue]
>
> and here is my constructor :
>
> SString::SString (int capacity=50):capacity_()
>
> {
> capacity_=capacity;
> }[/color]

Try this:

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

}
[color=blue]
>
> 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[/color]

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.


--
Clark S. Cox, III
clarkcox3@gmail.com

Slugger819
Guest
 
Posts: n/a
#4: Jul 23 '05

re: I need Help please


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

Abe

Thomas Matthews
Guest
 
Posts: n/a
#5: Jul 23 '05

re: I need Help please


Slugger819 wrote:[color=blue]
> Thanks a lot guys , I know there is a string class , but I'm trying to
> create my own
>
> Abe
>[/color]

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

Closed Thread


Similar C / C++ bytes