defining insertion operator

Pages: 12
yeah, it runs the program, but it ends up giving me a debug error after i try to input words that replace specific words within the paragraph in the stream. (such as animal, place, noun1, etc...)

 
return replaceWith;


that's what i changed it too, but of course it isn't right. don't think, to be perfectly honest with you, i was ever taught specifically about this. anyway to get some more help from you guys? seems this is the last thing i need to do before the program works
so ya, i've been messing around with this, and i can't seem to grasp what i'm doing wrong in replaceWord function.
Sorry, we seem to be in different time zones. For me, you write your posts at 4am.

i think you want to replace all words mathing "toReplace" with "replaceWith". It makes no sense to set toReplace = replaceWith and returning it then ..

try something like this:

1
2
3
4
5
6
void Paragraph::replaceWord(string toReplace, string replaceWith)
{
	for (int i = 0; i < wordCount; i++)  // wordCount is a private member
		if (toReplace == words[i])     // go through all words and check if it matches toReplace
			words[i] = replaceWith; // if yes, set it to replaceWith
}


Normally if you use objects of more complex classes which you do not change in a function you do something like this:

 
void Paragraph::replaceWord(const string & toReplace, const string & replaceWith)


Like this you do not copy the hole object, instead you just copy the 4 byte length address of your object, when calling the function.
Last edited on
that's alright.

i tried that code, but it gives 2 errors:

Error 1 error C2556: 'void Paragraph::replaceWord(std::string,std::string)' : overloaded function differs only by return type from 'std::string &Paragraph::replaceWord(std::string,std::string)' f:\advancedc++\project 6 (bad libs) chris nelson\bad libs project\main.cpp 208

Error 2 error C2040: 'Paragraph::replaceWord' : 'void (std::string,std::string)' differs in levels of indirection from 'std::string &(std::string,std::string)' f:\advancedc++\project 6 (bad libs) chris nelson\bad libs project\main.cpp 208
yes, because you still have to change the declaration in your class. I used no return type, whereas you did.

You still have a hard time to interpret the error messages! Try to think the cryptic things away and try to decipher what they are telling you.
Last edited on
Topic archived. No new replies allowed.
Pages: 12