C++ arrays on hte free store

class String
    {
    public:
        //Constructors
        String();
        String(const char *const);
        String(const String &);
        ~String();
   
    private:
        String(int);
        char * itsstring;
        unsigned short itslen;
   
   
    };

String::String()
    {
        cout << "Making String Class" << endl;
    }
 
String::String(int len)
    {
        itsstring = new char[len+1];
        for(int i=0;i<len;i++)
            itsstring[i] = "\0";                    //ERROR
        itsstring[len] = "\0";                    //ERROR
        itslen=len;
        cout << "\tString(int) constructor" << endl;
    }    


In the previous piece of code, my compier comes up with an error on the two lines with the ERROR comments.

It says "invalid conversion from `const char*' to `char' ".

if i try using the indirection operator to access the value stored at where the pointer points to on the free store then it comes up with a different error.

It then says "invalid type argument of `unary *'".

Please could someone help me

Thanks
IrAaMnVdEeRyYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
Try this:

itsstring[i] = '\0';

or:

itsstring[i] = 0;

itsstring[i] is char, and you are trying to assign char* to it.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AxterCommented:
FYI:
I recommend  you make the len arguement, the same type as the itslen argument.

String::String(unsigned short len)

Some compilers will give you a warning if you assigned an int to an unsigned short.
0
mnashadkaCommented:
A more efficient way of doing your loop:
for(int i=0;i<len;i++)
            itsstring[i] = "\0";                    //ERROR
        itsstring[len] = "\0";                    //ERROR

is:
memset(itsstring, 0, len + 1); // Set all members to 0
0
itsmeandnobodyelseCommented:
Actually, the length argument of a string class should count the number of non-zero characters as the zero character normally '\0' terminates the string. Most string classes have two lenght members, one for allocated length and one for string length. Then, the string buffer must be only reallocated when the string is growing. I agree with Axter that length type of argument and member should be same, but would recommend to change to int type rather than to a short or unsigned short as the int type is the best supported and most usual type. When using short type as an argument, you'll often get compiler warnings stating that converting an int to a short may leed to a loss of information. Though, that warning rarely is true, you could avoid that by choosing int as member type.

class String
    {
    public:
        //Constructors
        String();
        String(const char *const);
        String(const String &);
        ~String();
   
    private:
        String(int);
        char * itsstring;
        int itslen;
        int itsalloc;
    };

String::String()
    {
        cout << "Making String Class" << endl;
    }

// allocate a string buffer fro an empty string  
String::String(int alloc)
   : itslen(0), itsalloc(alloc)
    {
        itsstring = new char[alloc];
        itsstring[0] = '\0';   // It is sufficient to set the terminating zero character      
        cout << "\tString(int) constructor" << endl;
    }    

Regards, Alex


0
SkonenCommented:
On another note, you're not freeing the memory you allocated to itsstring, it'd be wise to delete it in your deconstructor:


....

        // Constructors
        String();
        String(const char *const);
        String(const String &);

        // Deconstructor
        ~String() { if (itsstring) delete [] itsstring; }
   
 ....


Good luck,
      Stuart Konen
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.