Why is the output not working properly?

Why is the output not working properly? I am sure I have copied it exactly from my book.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  #include <iostream>
#include <string.h>

// Rudimentary string class

class String
{
public:
    //constructors
    String();
    String (const char * const);
    String (const String &);
    ~String();
    
    //overloaded operators
    char & operator[](int offset);
    char operator[](int offset) const;
    String operator+(const String&);
    friend String operator+(const String&, const String&);
    void operator+=(const String&);
    String & operator= (const String &);
    
    // General accessors
    int GetLen()const {return itsLen;}
    const char * GetString() const { return itsString;}
    
private:
    String(int);
    char * itsString;
    unsigned short itsLen;
};

// default constructor creates a string of 0 bytes
String::String()
{
    itsString = new char[1];
    itsString[0]='\0';
    itsLen=0;
    //std::count <<"\tDefault string constructor\n";
    //ConstructorCount++;
}

// Converts a character array to a string
String::String(const char * const cString)
{
    itsLen = strlen(cString);
    itsString = new char[itsLen+1];
//std::cout<<"\tString(char*) constructor\n";
// ConstructorCount++
}

//opy constructor
String::String(const String & rhs)
{
    itsLen=rhs.GetLen();
    itsString = new char[itsLen+1];
    for (int i=0; i<itsLen;i++)
        itsString[i] = rhs[i];
        itsString[itsLen] = '\0';
        //std::cout<<"\tString(String&) constructor\n";
        //ConstructorCount++;
}

// destructor, frees allocated memory
String::~String ()
{
    delete [] itsString;
    itsLen = 0;
    // std::cout <<"\tString destructor\n";
}

// non constant offset operator, returns
// reference to character so it can 
// be changed!

char & String::operator[](int offset)
{
    if (offset > itsLen)
        return itsString[itsLen-1];
        else
            return itsString[offset];
}

// constant offset operator for use
// on const objects (see copy constructor!)
char String::operator[](int offset) const
{
    if (offset > itsLen)
        return itsString[itsLen-1];
        else
            return itsString[offset];
}

// creates a new string by adding current
// string to rhs
String String::operator+(const String & rhs)
{
    int totalLen = itsLen + rhs.GetLen();
    String temp(totalLen);
    for(int i = 0; i<itsLen; i++)
        temp[i] = itsString[i];
        for(int j = 0; j<rhs.GetLen(); j++,i++)
            temp[i] = rhs[j];
            temp[totalLen]='\0';
            return temp;
}

// creates a new string by adding
// one string to another
String operator+(const String& lhs, const String& rhs)
{
    int totalLen=lhs.GetLen() + rhs.GetLen();
    String temp(totalLen);
    for (int i = 0 ;i<lhs.GetLen(); i++)
        temp[i]=lhs[i];
        for (int j=0; j<rhs.GetLen(); j++, i++)
            temp[i]=rhs[j];
            temp[totalLen]='\0';
            return temp;
}

// operator equals, frees existing memory
// then copies string and size
String& String::operator=(const String & rhs)
{
    if (this == &rhs)
        return *this;
        delete[] itsString;
        itsLen=rhs.GetLen();
        itsString = new char[itsLen+1];
        for(int i =0; i<itsLen;i++)
            itsString[i]=rhs[i];
            itsString[itsLen] = '\0';
            return *this;
            //std::cout<<"\tString operator=\n";
}

// private (helper) constructor, used only by
// class function members for creating a new string of
// required size. Null filled
String::String(int len)
{
    itsString = new char[len+1];
    for (int i=0; i<len; i++)
        itsString[1] ='\0';
        itsLen=len;
        //std::cout<<"\tString(int) constructor\n";
        // ConstructorCount++;
}

int main()
{
    String s1("String One");
    String s2("String Two");
    char *c1 = { "C-String One" } ;
    String s3;
    String s4;
    String s5;
    
    std::cout <<"s1" << s1.GetString() << std::endl;
    std::cout <<"s2" << s2.GetString() << std::endl;
    std::cout <<"c1" << c1 << std::endl;
    s3 = s1 + s2;
    std::cout << "s3" << s3.GetString()<< std::endl;
    s4 = s1 + c1;
    std::cout << "s4" << s4.GetString() << std::endl;
    s5 = c1 + s1;
    std::cout <<"s5" << s5.GetString() << std::endl;
}
 In member function 'String String::operator+(const String&)': 102:44: error: 'i' was not declared in this scope
 In function 'String operator+(const String&, const String&)': 116:44: error: 'i' was not declared in this scope


Please don't ignore the error messages that your compiler gives you.

You are missing two pairs of curly braces { } to delimit the i loops. (At least)
Please show me what lines I am missing curly braces as I have copied this code exactly from my book. Thanks.
Actually, I was thrown by your very poor indentation in String::operator+ (lines 100 to 105), and the other operator+ (lines 114 to 119).

On closer inspection, these appear to be two separate (non-nested) loops in each case, but your indentation is wrong and obscuring that. You need to both declare and initialise i on both lines 102 and 116 or i will not exist.

I would strongly advise you to correct your indentation in both these functions, and, although a loop consisting of one statement doesn't strictly need enclosing curly brackets, if you put that statement on a separate line then I suggest you enclose it with { }, especially with dodgy indentation.

Line 145 is also wrong. You have probably mistyped i as 1. Please sort your indentation out.



Last edited on
Topic archived. No new replies allowed.