error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const String2'

Hey All,
Im trying to rewrite parts of the string class for lessons in using new and delete with classes. I am however running into a problem with my overloaded operator>> function. It seems to me like the compiler is saying there is ambiguity but I dont know how to resolve that. Here is the code:

String2.h
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
#ifndef STRING2_H
#define STRING2_H
#include <iostream>

class String2
{
private:
	char * str;
	int len;
	static int num_strings;
	static const int CINLIM = 80;
public:
	//Constructors, destructors, etc...
	String2(const char * s);
	String2();
	String2(const String2 &);
	~String2();
	int length() const {return len;};
	void stringlow();
	void stringup();
	int cCount(char c);

	//overloaded operator methods
	String2 & operator=(const String2 &);
	String2 & operator=(const char *);
	String2 operator+(String2 & st);
	char & operator[](int i);
	const char & operator[](int i) const;

	//overloaded operator friends
	friend bool operator<(const String2 & st, const String2 & st2);
	friend bool operator>(const String2 & st, const String2 & st2);
	friend bool operator==(const String2 & st, const String2 & st2);
	friend String2 operator+(const char * c, String2 & st);
	friend std::ostream & operator<<(std::ostream & os, const String2 & st);
	friend std::istream & operator>>(std::istream & is, const String2 & st);

	//static function
	static int HowMany();
};
#endif 



String2.cpp
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 "String2.h"
#include <string.h>
using std::cin;
using std::cout;

int String2::num_strings = 0;

//static method

int String2::HowMany()
{
	return num_strings;
}

//class methods

String2::String2(const char * s)
{
	len = strlen(s);
	str = new char[len+1];
	std::strcpy(str, s);
	num_strings++;
}

String2::String2()
{
	str = new char [1];
	str[0] = '\0';
	len = 4;
	num_strings++;
}

String2::String2(const String2 & st)
{
	num_strings++;
	len = st.len;
	str = new char[len+1];
	std::strcpy(str, st.str);
}

String2::~String2()
{
	--num_strings;
	delete [] str;
}

void String2::stringlow()
{
	char * temp;
	temp = new char[len + 1];
	strcpy(temp, str);
	for(int iii = 0; iii < len; iii++)
		if(isalpha(temp[iii]))
			temp[iii] = tolower(temp[iii]);
	strcpy(str, temp);
	
}

void String2::stringup()
{
	char * temp;
	temp = new char [len + 1];
	strcpy(temp, str);
	for(int iii = 0; iii < len; iii++)
		if(isalpha(temp[iii]))
			temp[iii] = toupper(temp[iii]);
	strcpy(str, temp);
}

int String2::cCount(char c)
{
	int count = 0;
	for(int iii = 0; iii < len; iii++)
		if(str[iii] == c)
			count++;
	return count;
}


//overloaded operator methods
String2 & String2::operator=(const String2 & st)
{
	if(this == &st)
		return *this;
	delete[] str; //REMEMBER TO DELETE WHAT THE CURRENT STRING IS POINTING TOO!!!
	len = st.len;
	str = new char[len + 1];
	strcpy(str, st.str);
	return *this;
}

String2 & String2::operator=(const char * c)
{
	delete[] str;
	len = std::strlen(c);
	str = new char[len + 1];
	std::strcpy(str, c);
	return *this;
}

//combining two strings
String2 String2::operator+(String2 & st)
{
	String2 temp;
	temp.len = len + st.len + 1;
	temp.str = new char[temp.len];
	strcpy(temp.str, str);
	strcat(temp.str, st.str);

	return temp;
}

//Combining a string and a literal string;
String2 operator+(const char * c, String2 & st)
{
	String2 temp;
	temp.len = st.len + strlen(c) + 1;
	temp.str = new char [temp.len];
	strcpy(temp.str, c);
	strcat(temp.str, st.str); //Append one string to the end of the other

	return temp;
}


//read write access for non-const String
char & String2::operator[](int i)
{
	return str[i];
}
 
//read access for const string
const char & String2::operator[](int i) const
{
	return str[i];
}

//operator overloading friends
bool operator<(const String2 & st, const String2 & st1)
{
	return (std::strcmp(st.str, st1.str) < 0);
}

bool operator>(const String2 & st, const String2 & st1)
{
	return (st1.str < st.str);
}

bool operator==(const String2 & st, const String2 & st1)
{
	return (std::strcmp(st.str, st1.str) == 0);
}

std::ostream & operator<<(std::ostream & os, const String2 & st)
{
	os << st.str;
	return os;
}

std::istream & operator>>(std::istream & is, const String2 & st)
{
	char temp[String2::CINLIM];
	is.get(temp, String2::CINLIM);
	if(is)
		st = temp; //THIS RETURNS THE ERROR!! :(
	while(is && is.get() != '\n')
		continue;
	return is;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "String2.h"

int main()
{
	String2 s1(" and I am a C++ student.");
	String2 s2 = "Please enter your name: ";
	String2 s3;
	std::cout << s2;
	std::cin >> s3;
	s2 = "My name is " + s3;

	return 0;
}
The reason you are getting an error is because st is constant to fix the error remove the const from const String2& st.
Topic archived. No new replies allowed.