assignment operator

hey guys, i'm working in streams (as i stated in an earlier post), and i'm trying to define the assignment operator.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
Paragraph &Paragraph::operator =(const Paragraph &para)
{
	if(this != &para)
	{
		delete [] para.words;

		words = new string[wordCount];
		wordCount = para.wordCount;

		for (int i=0; i < para.wordCount; i++)
			words[i] = para.words[i];
	}
	return *this;
}


*edit for adding code tags

that's what i have so far, but i keep getting a few errors, such as :

Error 1 error C2556: 'Paragraph &Paragraph::operator =(const Paragraph &)' : overloaded function differs only by return type from 'void Paragraph::operator =(const Paragraph &)' f:\advancedc++\project 6 (bad libs) chris nelson\bad libs project\main.cpp 179

Error 2 error C2040: 'Paragraph::operator =' : 'Paragraph &(const Paragraph &)' differs in levels of indirection from 'void (const Paragraph &)' f:\advancedc++\project 6 (bad libs) chris nelson\bad libs project\main.cpp 179

i'm getting other errors as well, but it's with defining another function, don't want to ask too many questions at once. thanks in advance for help
Last edited on
I'm guessing your definition and your declaration aren't the same. The definition in the class seems like it has void as it's return type, but the one you posted returns a Paragraph reference.
1
2
words = new string[wordCount];
wordCount = para.wordCount;


Look closely. This is wrong.

(logically, not syntactically. This won't cause compiler errors, but it will cause broken code)
(your compiler errors are being caused by something else)
Last edited on
hmmmmmm, those 2 errors i gave you guys point the the first open curly brace in the code. i was thinking those 2 errors had to deal with that. i'm having a tough time understanding these concepts lately, just trying to get extra help. should i post all my code for you guys to look at? i have like 8 errors in total right now. would you guys be willing to analyze my code and kindly let me know my problem areas and a nudge in the right direction?
Go ahead, if it's really long (like more than 2 screen lengths), post it somewhere like http://pastebin.com
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

// Paragraph class definition.
class Paragraph
{
public:
	Paragraph();
	Paragraph(const Paragraph &para);  // copy constructor
	~Paragraph();

	static void setLineLength(int len);

	string &replaceWord(string toReplace, string replaceWith);

	friend ostream &operator<<(ostream &outStream, const Paragraph &para);
	friend istream &operator>>(istream &inStream, const Paragraph &para);

	void operator =(const Paragraph &para);

	int numWords() const;

	string getWord(int i) const;
	// Precondition: i is zero-based word index: 0 <= i <= numWords()-1
	
private:
	static int lineLength;
	string *words;	// POINTER TO DYNAMICALLY-ALLOCATED ARRAY
	int wordCount;	// ARRAY SIZE
};


// Prototype for function dealing with paragraphs.

const Paragraph fillInWords(Paragraph para);


int main()
{
	Paragraph para;

	// LOAD PARAGRAPH FROM FILE (FIND FILE WITHIN PROJECT FOLDER).
	ifstream paraStream("paragraph.txt");
	paraStream >> para;
	paraStream.close();

	Paragraph versions[2];

	cout << "\nFill in words (no spaces):" << endl;
	versions[0] = fillInWords(para);
	cout << "\nVersion1:" << endl;
	cout << versions[0] << endl;

	cout << "\nFill in different words (no spaces):" << endl;
	versions[1] = fillInWords(para);
	cout << "\nVersion2:" << endl;
	cout << versions[1] << endl;

	int favoriteNum;
	do
	{
		cout << "\nYour favorite version (1 or 2)? ";
		cin >> favoriteNum;
		if (favoriteNum == 1 || favoriteNum == 2)
			break;
		cerr << "Unknown favorite #" << favoriteNum << endl;
	}
	while (true);	

	// SAVE FAVORITE VERSION OF BAD LIB TO FILE "favorite.txt".

	

	cout << "\nPress Enter to exit.";
	// Read a char (any input followed by Enter suffices).
	cin.get();

	return 0;
}


// Definition for function dealing with paragraphs.

const Paragraph fillInWords(Paragraph &para)  // PASS BY VALUE
{
	int i;
	for (i = 0; i < para.numWords(); i++)
	{
		string word = para.getWord(i);

		if (word[0] == '<')
		{
			string replaceWith;
			cout << word << ": ";
			cin >> replaceWith;
			cin.ignore();  // swallow newline
			para.replaceWord(word, replaceWith);
		}
	}

	return para;  // COPY IS RETURNED
}

// Paragraph static member definitions.

int Paragraph::lineLength = 66;

void Paragraph::setLineLength(int len)
{
	lineLength = len;
}

// Paragraph member function definitions.

Paragraph::Paragraph()
: wordCount(0), words(NULL)
{
}

Paragraph::~Paragraph()
{
	delete [] words;
	words = NULL;
	wordCount = 0;
}

ostream &operator <<(ostream &outStream, const Paragraph &para)
{
	// Output number of words in paragraph.
	outStream << para.wordCount << endl;
	int lengthLine = 0;


	int i;

	for (i = 0; i < para.wordCount; i++)
	{
		if (i > 0)
			outStream << ' ';
		
		
	}

	return outStream;
}

int Paragraph::numWords() const
{
	return wordCount;
}

string Paragraph::getWord(int i) const
{
	return words[i];
}

Paragraph::Paragraph(const Paragraph &para)
{
	words = new string[wordCount];
	wordCount = para.wordCount;

	int i;
	for (i=0; i < wordCount; i++)
		words[i] = para.words[i];
}

Paragraph &Paragraph::operator =(const Paragraph &para)
{
	if(this != &para)
	{
		delete [] para.words;

		words = new string[wordCount];
		wordCount = para.wordCount;

		for (int i=0; i < para.wordCount; i++)
			words[i] = para.words[i];
	}
	return *this;
}


istream &operator >>(istream &inStream, const Paragraph &para)
{
	inStream >> para.wordCount;

	delete [] para.words;  //deallocates arrray

	para.words = new string[para.wordCount];

	string tempPara;
	for(int i = 0; i < wordCount; i++)
		istream >> tempPara.words[i];

	return inStream;

}

string &Paragraph::replaceWord(string toReplace, string replaceWith)
{
	toReplace = replaceWith;
	return replaceWith;
}


*edited for adding code tags

this is all my code so far. i'm sure i've made plenty of errors, but i'm here to learn!
Last edited on
anybody have advice?
First advice, put your code inside code tags. It provides formatting to make the code easier to read, and it also provides line numbering to make it easier for us to point you to the lines that are problems.

Second, as firedraco said, your declaration of the assignment operator in your Paragraph class returns type void, where your definition returns type Paragraph. These need to match. I'd provide specific line numbers if they were available.
Last edited on
i changed the assignment operater in the class to not return type void. now i'm having problems with the extraction operator lines 187-201. i'm getting 5 errors that are all in that definition.
1
2
3
istream &operator >>(istream &inStream, const Paragraph &para)  // if 'para' is const
{
	inStream >> para.wordCount;  // then you can't modify it 


You should be passing para as non-const reference here (ie: remove the const keyword). The objective of the >> operator is to change it, therefore you shouldn't pass it as const.

Also:

1
2
		words = new string[wordCount];
		wordCount = para.wordCount;


This is still broken. (how many strings are you allocating there?)
Topic archived. No new replies allowed.