string class part two

So im building my own personal string class called DTSString but for some reason I couldnt get it to print on the standard output stream so i looked up overloading the operators << and >> and found out how to do it, but its still not printing anything. im using Xcode with gcc

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdio>
#include <cstring>

class DTSString
{
    char* currentStringValue;
    
    public:

        DTSString(const char* initialConstructorValue) { strcpy(currentStringValue, initialConstructorValue); }
    
        friend DTSString operator+(DTSString stringOne, DTSString stringTwo)
        {
            unsigned long length = strlen(stringTwo.currentStringValue);
            return DTSString(std::strncat(stringOne.currentStringValue, stringTwo.currentStringValue, length));
        }
    
        friend DTSString operator*(DTSString stringOne, int max)
        {
            int counter = 1;
            unsigned long length = strlen(stringOne.currentStringValue);
            char* returnString = 0;
            
            while(counter <= max)
            {
                std::strncat(returnString, stringOne.currentStringValue, length);
                counter = counter + 1;
            }
            
            return returnString;
        }
    
        friend std::ostream &operator<<(std::ostream &out, DTSString stringOut)
        {
            out<< stringOut << std::endl;
            return out;
        }
            
        friend std::istream &operator>>(std::istream &in, DTSString &stringIn)
        {
            in>> stringIn;
            return in;
        }

};
this way you create an infinite recursion.

What you want to in/output is currentStringValue
so on lines 36 and 42 I want to add .currentStringValue?
oh i guess i did that last night but i don't remember doing that. it still doesnt work
so on lines 36 and 42 I want to add .currentStringValue?
Yes

it still doesnt work
What doesn't work?

line 42 is more complicated since you need to know how many characters need to be stored in currentStringValue

currentStringValue is, by the way, the reason why you made the operators << >> friend function
Nothing is going to "work" until you fix that constructor. You don't allocate any memory to store your string, so you're trampling memory you don't own.
Topic archived. No new replies allowed.