fork download
  1. #include <iostream>
  2. using namespace std;
  3. #define WIDTH 8
  4.  
  5. class Word {
  6. char* text;
  7. int nletters;
  8. public:
  9. Word() {
  10. text = NULL;
  11. nletters = 0;
  12. cout << 'W';
  13. }
  14. explicit Word(const char* s) {
  15. nletters = 0;
  16. while(s[nletters] != '\0')
  17. nletters++;
  18. text = new char[nletters];
  19. for (int i = 0; i < nletters; i++)
  20. text[i] = s[i];
  21. cout << 'X';
  22. }
  23. Word(const Word& p) {
  24. text = NULL;
  25. *this = p; // calls assignment operator below
  26. cout << 'Y';
  27. }
  28. ~Word() {
  29. if (text != NULL) delete [] text;
  30. cout << "~W" << nletters;
  31. }
  32. Word& operator=(const Word& p) {
  33. if (this != &p) {
  34. if (text != NULL) delete [] text;
  35. nletters = 0;
  36. if (p.text != NULL) {
  37. text = new char[p.nletters];
  38. while(nletters < p.nletters) {
  39. text[nletters] = p.text[nletters];
  40. nletters++;
  41. }
  42. }
  43. else
  44. text = NULL;
  45. }
  46. cout << 'V';
  47. return *this;
  48. }
  49. int nLetters() const { return nletters; }
  50. friend ostream& operator<<(ostream& os, const Word& w) {
  51. if (w.text != NULL)
  52. for (int i = 0; i < w.nletters; i++)
  53. os << w.text[i];
  54. else
  55. os << "***";
  56. os << ' ';
  57. return os;
  58. }
  59. };
  60.  
  61. class Paragraph {
  62. Word* word;
  63. int mwords;
  64. int nwords;
  65. int width;
  66. public:
  67. Paragraph() {
  68. word = NULL;
  69. width = WIDTH;
  70. nwords = 0;
  71. mwords = 0;
  72. cout << 'P' << endl;
  73. }
  74. Paragraph(const Paragraph& c) {
  75. word = NULL;
  76. *this = c; // calls assignment operator below
  77. cout << "cP";
  78. }
  79. Paragraph& operator=(const Paragraph& c) {
  80. if (this != &c) {
  81. if (word != NULL) delete [] word;
  82. if (c.word != NULL) {
  83. word = new Word[c.mwords];
  84. cout << endl;
  85. for (int i = 0; i < c.nwords; i++)
  86. word[i] = c.word[i]; // calls Word assignment operator
  87. }
  88. else
  89. word = NULL;
  90. width = c.width;
  91. nwords = c.nwords;
  92. mwords = c.mwords;
  93. }
  94. cout << "=P";
  95. return *this;
  96. }
  97. ~Paragraph() {
  98. if (word != NULL) delete [] word;
  99. cout << '\n' << nwords << "~P" << endl;
  100. }
  101. void make(int m) {
  102. if (word == NULL) {
  103. word = new Word[m];
  104. mwords = m;
  105. nwords = 0;
  106. width = WIDTH;
  107. }
  108. }
  109. void setWidth(int w) { width = w; }
  110. friend ostream& operator<<(ostream& os, const Paragraph& p) {
  111. int nextWord = 0;
  112. for (int i = 0; i < p.nwords; i++) {
  113. if (nextWord + p.word[i].nLetters() > p.width) {
  114. os << '\n';
  115. nextWord = 0;
  116. }
  117. cout << p.word[i];
  118. nextWord += p.word[i].nLetters() + 1;
  119. }
  120. return os;
  121. }
  122. Paragraph& operator+=(const char* w) {
  123. if (nwords < mwords) {
  124. cout << "\n+=";
  125. word[nwords] = w; // calls Word constructor, then Word = operator
  126. nwords++;
  127. }
  128. return *this;
  129. }
  130. };
  131.  
  132. int main() {
  133.  
  134. Paragraph p;
  135. cout << "--------\n";
  136. p.make(5);
  137. p += "This";
  138. p += "is";
  139. p += "hard";
  140. cout << "\n--------\n";
  141. cout << p << endl;
  142. cout << "--------\n";
  143. Paragraph q = p;
  144. q.setWidth(6);
  145. q += "too";
  146. cout << "\n--------\n";
  147. cout << q << endl;
  148. cout << "---------------\n";
  149. return 0;
  150. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘Paragraph& Paragraph::operator+=(const char*)’:
prog.cpp:125: error: no match for ‘operator=’ in ‘*(((Paragraph*)this)->Paragraph::word + ((unsigned int)(((unsigned int)((Paragraph*)this)->Paragraph::nwords) * 8u))) = w’
prog.cpp:32: note: candidates are: Word& Word::operator=(const Word&)
stdout
Standard output is empty