I need help on my problem...

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
String::String(int maxsize)
{
	fstr = new char[maxsize];
	fcap = maxsize; 
	fstr[0] = '\0'; 
	flen = 0; 

}
String::String(const String &s) : flen(s.flen), fcap(s.fcap)
{
	fstr = new char[fcap];
	if (fstr != 0)
	{
	
		for (int i = 0; i < flen; i++)// flen size of the array 
		{
			fstr[i] = s.fstr[i];
		}
	}
	else
	{
		cerr << "inadequate memory to allocate storage for list" << endl; 
		exit(1);
	}

}
const String & String :: operator = (const String &s)
{
	if (this != &s)
	{
		if (fcap != s.fcap)
		{
			delete[] fstr; 
			fcap = s.fcap; 
			fstr = new char[fcap];
		}
		if (fstr == 0)
		{
			cerr << "Inadequate memory to allocat stack\n";
			exit(1); 
		}
	}
	return *this; 
}
String :: ~String()
{
	delete[] fstr; 
}

String String :: operator += (const String &str)// append 
{
	flen += str.flen; 
	fstr += str.fstr; 
	return *this; 
}

is my coding
one below is part of driver
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
	
String str1, str2("init2"), str3 = "init3"
char s1[100], s2[100], s3[100];    
cout << "\nEnter a value for str1 (no spaces): ";
	cin >> s1;
	str1 = s1;

	cout << "\nEnter a value for str2 (no spaces): ";
	cin >> s2;
	str2 = s2;

	cout << "\nEnter a value for str3 (no spaces): ";
	cin >> s3;
	str3 = s3;


	cout << "\nAfter assignments..." << endl;
	PrintString("str1", str1);
	PrintString("str2", str2);
	PrintString("str3", str3);

	// Access some elements...

	int i;

	cout << "\nEnter which element of str1 to display: ";
	cin >> i;
	cout << "Element #" << i << " of str1 is '" << str1[i]
		<< "'" << endl;

	cout << "\nEnter which element of str2 to display: ";
	cin >> i;
	cout << "Element #" << i << " of str2 is '" << str2[i]
		<< "'" << endl;

	cout << "\nEnter which element of str3 to display: ";
	cin >> i;
	cout << "Element #" << i << " of str3 is '" << str3[i]
		<< "'" << endl;

	// Append some strings...

	cout << "\nEnter a value to append to str1 (no spaces): ";
	cin >> s1;
	// str1.append(s1); // Actually, the cstring is converted to String object here by the constructor 
	str1 += s1;   // same as above 

	cout << "\nEnter a value to append to str2 (no spaces): ";
	cin >> s2;
	str2 += s2;


	cout << "\nEnter a value to append to str3 (no spaces): ";
	cin >> s3;
	str3 += s3;

On
"\nEnter a value for str1 (no spaces): ";
str1,2,3 = 1 i get error no operator "=" matches these operands operand types are : String = char[100]
On
String str1, str2("init2"), str3 = "init3"
no suitalbe constructor exist to convert from const char[6] to String
On str1,2,3 += s1,2,3
error: no operator "+=" matches these operands
operand types are: String += char[100]

I don't know what i did wrong Plz help and I have Error on
fstr += str.fstr;
1
2
3
4
5
6
String String :: operator += (const String &str)// append 
{
	flen += str.flen; 
	fstr += str.fstr; 
	return *this; 
}
Last edited on
The error messages are telling you what's wrong:

1) You haven't defined an assignment operator that can set your String class to be equal to a char array. The compiler can't magically know how to do this - you need to define it.

2) Similarly, you haven't defined a constructor that can take a char array as an argument.

3) Similarly, you haven't defined a += operator that can take a char array as an operand.

4) You can't just add two char arrays like that to make a longer array. You need to use the string manipulation functions in the standard C library.
Last edited on
Topic archived. No new replies allowed.