String Class

im making a string class, but i have some trouble making it. could some one please tell me what im doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class DTSString
{
    char* currentStringValue;
    
    public:
    DTSString(char *initialConstructorValue) { currentStringValue = initialConstructorValue; }
    
        //binary non-comparison operators
        friend DTSString operator+(DTSString, DTSString);
        friend DTSString operator+(DTSString, char);
        friend DTSString operator-(DTSString, DTSString);
        friend DTSString operator-(DTSString, char);
        friend DTSString operator*(DTSString, int);
        friend DTSString operator/(DTSString, int);
};
Is there an error here? And why would you have - * / operations? Also, you should make a copy of initialConstructorValue. Otherwise memory management will be a huge pain.
@Aramil of Elixia

You shouldn't use = on char*'s. Use strcpy() instead (#include <cstring> ).

If you use =, it copies the pointer rather than the actual data. This means that when the actual data is changed or is deallocated, the string object is affected as well when it shouldn't be.
the - / are implemented in a way that ill explain later. the * is like ruby strings. what do you mean make a copy? and yes. i tried making an object and initializing it and then printing it but it gave me an error
here is my updated code. its saying it cant find the function strcat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstring>


class DTSString
{
    char* currentStringValue;
    
    public:
        DTSString(const char* initialConstructorValue) { strcpy(currentStringValue, initialConstructorValue); }
    
        //binary non-comparison operators
        friend DTSString operator+(DTSString stringOne, DTSString stringTwo)
        {
            return DTSString(strcat(stringOne, stringTwo));
        }
    
        friend DTSString operator+(DTSString stringOne, char charOne)
        {
            char* appendChar = &charOne;
            
            return DTSString(strcat(stringOne, charOne));
        }
    
        friend DTSString operator-(DTSString, DTSString);
        friend DTSString operator-(DTSString, char);
        friend DTSString operator*(DTSString, int);
        friend DTSString operator/(DTSString, int);
};


and then in main when i go to test it, it says it cant put my variable onto the std output stream
1
2
3
4
5
6
7
8
9
10
11
#include "DTSString.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    DTSString testString("Hello, world!");
    
    cout<< testString << endl;
}
strcat lives in the std namespace in the header file cstring. Do you have appropriate using declarations?
so here is my revised code. now its saying it can't convert between DTSstring and char*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class DTSString
{
    char* currentStringValue;
    
    public:
        DTSString(const char* initialConstructorValue) { strcpy(currentStringValue, initialConstructorValue); }
    
        //binary non-comparison operators
        friend DTSString operator+(DTSString stringOne, DTSString stringTwo)
        {
            return DTSString(std::strcat(stringOne, stringTwo));
        }
    
        friend DTSString operator+(DTSString stringOne, char charOne)
        {
            char* appendChar = &charOne;
            
            return DTSString(std::strcat(stringOne, charOne));
        }
    
        friend DTSString operator-(DTSString, DTSString);
        friend DTSString operator-(DTSString, char);
        friend DTSString operator*(DTSString, int);
        friend DTSString operator/(DTSString, int);
};
Well that seems like a reasonable complaint. The strcat command accepts objects of type char-pointer, and you're trying to feed it objects of type DTSString. What did you think would happen?
well what am i supposed to do to add the strings together then? becuase DTSString is technically a char* with just more features
well what am i supposed to do to add the strings together then?


You're supposed to pass strcat two char* objects. Is that not obvious from reading the strcat documentation?

1
2
3
4
5
6
  friend DTSString operator+(DTSString stringOne, char charOne)
        {
             // Here ytou will need to do something to make sure stringOne.currentStringValue has enough space to add this one extra char you want to add
            std::strcat(stringOne.currentStringValue, &charOne);
            return srtingOne;
        }


becuase DTSString is technically a char* with just more features

EVERY object is technically some basic objects with more features. It's up to you to tell the compiler what to do with them.
Last edited on
the - / are implemented in a way that ill explain later.

If you have to explain it at all, then it's an abuse of operator overloading.
It should be a normal function.
yeah i realized that i just need to pass the char *'s instead of the object. so now i have two questions. it lets me add two DTSStrings together but that was because i overloaded it, but why does it let me put the value in a third one with the = operator w/o overloading it? and my second is do i have to overload the << operator to cout it? and if so how?
Topic archived. No new replies allowed.