Can anyone help me fix these 8 errors on this string class

can anybody help me fix these 8 errors on this string class.
Ive tried to copy it out of my programming book word for word and checked it again but I can't find any other syntax errors can somebody help me spot them.

console output:

/bin/sh -c '/usr/bin/make -j2 -e -f "TheStringClass.mk" MakeIntermediateDirs && /usr/bin/make -j2 -e -f "TheStringClass.mk" ./Debug/string.cpp.o'
----------Building project:[ TheStringClass - Debug ] (Single File Build)----------
/usr/bin/g++ -c "/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp" -g -O0 -Wall -o ./Debug/string.cpp.o -I. -I.
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:22:9: error: expected unqualified-id before 'char'
const * char GetString() const { return itsString; }
^
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:22:7: error: expected ';' at end of member declaration
const * char GetString() const { return itsString; }
^
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp: In member function 'char String::GetString() const':
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:22:41: error: invalid conversion from 'char*' to 'char' [-fpermissive]
const * char GetString() const { return itsString; }
^
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp: At global scope:
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:33:1: error: redefinition of 'String::String()'
String::String()
^
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:8:5: note: 'String::String()' previously defined here
String(){};
^
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp: In copy constructor 'String::String(const String&)':
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:71:5: error: expected ';' before 'for'
for (int i =0;i<itsLen;i++)
^
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:71:19: error: 'i' was not declared in this scope
for (int i =0;i<itsLen;i++)
^
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp: In member function 'String String::operator+(const String&)':
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:131:43: error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]
for (int j=0;j<rhs.GetLen(); j++, i++)
^
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:131:43: note: (if you use '-fpermissive' G++ will accept your code)
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp: In member function 'void String::operator+=(const String&)':
/home/alistair/Codelite/AnIntroductionToProgramming/TheStringClass/TheStringClass/string.cpp:145:44: error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]
for (int j =0; j<rhs.GetLen(); j++,i++)
^
TheStringClass.mk:95: recipe for target 'Debug/string.cpp.o' failed
make: *** [Debug/string.cpp.o] Error 1
====8 errors, 2 warnings====


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
#include <iostream>
#include <string.h>

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&);
void operator+=(const String &);
String & operator= (const String &);

// General accessors
int GetLen()const { return itsLen; }
const * char GetString() const { return itsString; }
// static int ConstructorCount;

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::cout<< "\tDefault string constructor\n";
    // ConstructorCount++;
}
    // 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++;
}

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

// copy 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";
}

// 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";
}

// 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;
}

// changes current string, return nothing
void String::operator+=(const String& rhs)
{
    unsigned short rhsLen = rhs.GetLen();
    unsigned short totalLen = itsLen + rhsLen;
    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[i-itsLen];
            temp[totalLen]='\0';
            *this = temp;
}

// int String::ConstructorCount = 0; 
1.
const * char doesn't make sense.
See: https://stackoverflow.com/questions/890535/what-is-the-difference-between-char-const-and-const-char
Maybe you want const char* or const char * const.

2.
Line 8: String(){};
You declare and define the body of the 0-arg ctor.
Line 33: You are trying to define the body again.

3. Curly braces are needed around for loops and if statements if you have more than one statement!!!
1
2
3
4
5
for (int i = 0; i < itsLen; i++)
{
    multiple;
    statements;
}


Last edited on
Topic archived. No new replies allowed.