【发布时间】:2015-10-08 18:19:06
【问题描述】:
我的目标是重载“+”运算符,这样我就可以组合段落对象和故事对象。此函数应该返回一个新的 Story 对象,并在开头附加段落。
Story Paragraph::operator+(const Story& story) {
Paragraph paragraph;
Story stry;
Paragraph storyPara = story.paragraph;
Sentence paraSentence = storyPara.sentence;
paragraph.sentence = this->sentence + paraSentence;
stry.paragraph = paragraph;
return stry;
}
但是,当我运行所有代码时(Story 对象应该有一个段落。Paragraph 对象应该有一个句子。Sentence 对象应该有一个单词,等等),我得到了这个错误:
错误:没有可行的重载'='
当我尝试执行以下操作时会发生这种情况:
paragraph.sentence = this->sentence + paraSentence;
我不太确定如何将句子组合在一起形成一个段落(最终形成并返回一个新故事)。有谁知道如何解决这个问题?
【问题讨论】:
-
“你可以假设我所有的类都被正确定义了” 如果这是真的,你就不会出错...
-
Sentence类是否有复制构造函数或重载的=运算符? -
什么问题?我们看不到任何相关的代码。出示您过去几天用来调试问题的minimal testcase。
-
告诉我们
Sentence的定义。 -
而
Word是...?另外,段落不是一个或多个句子吗?那么为什么Paragraph中只有一个Sentence对象?
标签: c++ operator-overloading overloading addition