Firstly, my code:
I have two problems, first when I try to compile the above code, with MS Visual C++, I get the following error:Code:#include <iostream> using namespace std; class text { char *str; public: void setText(char *s, bool memUsed); //void setText(char *s, bool memUsed); char* getText() { return str; } int length() { return strlen(str); } text(char *s = "Default Constructor") { setText(s, false); cout << "\nConstructing\n"; } ~text() { delete [] str; cout << "\nDestructing\n"; } text(const text &s); char &letter(int let); }; void text::setText(char *s, bool memUsed = true) { if(memUsed == true) { delete [] str; } int length = strlen(s) + 1; str = new char[length]; if(!length) { cout << "Allocation Error"; exit(1); } strcpy(str, s); } text::text(const text &s) { int length = strlen(s.str) + 1; str = new char[length]; if(!length) { cout << "Allocation Error"; exit(1); } strcpy(str, s.str); cout << "\nCopy Constructor\n"; } char &text::letter(int let) { if(let > 0 && let < length()) { return str[let]; } return str[0]; } int main() { text one("jason spaceman"); text two; text three = one; text ob; cout << one; cout << two; cout << three; cout << endl; one.letter(0) = 'J'; cout << one; cout << three; cout << ob; return 0; }
It gives me an error for each of the "cout <<" parts in main().G:\CPRG6101\Lab8b.cpp(73) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class text' (or there is no acceptable conversion)
Secondly, I want the program to change the first letter of my name (jason spaceman) to 'Jason Spaceman' (from a lowercase j to an uppercase J) and then output it to the screen along with the length of the string. What do I need to add to main() in order to do this?
Thanks for any help.



LinkBack URL
About LinkBacks


