简体   繁体   English

重载运算符 C++:错误:没有可行的重载“=”

[英]Overloading an operator C++: error: no viable overloaded '='

My goal is to overload the '+' operator so that I can combine a Paragraph object and a Story object.我的目标是重载“+”运算符,以便我可以组合 Paragraph 对象和 Story 对象。 This function should return a new Story object with the paragraph attached to the beginning.这个函数应该返回一个新的 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;
}

However, when I run all my code (A Story object should have a paragraph. A Paragraph object should have a sentence. A Sentence object should have a word, etc.) , I get this error:但是,当我运行我的所有代码时(一个 Story 对象应该有一个段落。一个段落对象应该有一个句子。一个 Sentence 对象应该有一个词等) ,我收到这个错误:

error: no viable overloaded '='错误:没有可行的重载“=”

This occurs when I try to do the following line:当我尝试执行以下行时会发生这种情况:

paragraph.sentence = this->sentence + paraSentence;

I'm not quite sure how to add the sentences together to form a paragraph (to ultimately form & return a new Story).我不太确定如何将句子加在一起形成一个段落(最终形成并返回一个新的故事)。 Does anyone know how to approach this problem?有谁知道如何解决这个问题?

you can assume that all my classes are defined properly

This is the wrong assumption which causes you this error.这是导致您出现此错误的错误假设。 Sentence class has apparently no or wrong operator= and/or copy constructor defined Sentence类显然没有或错误地定义了operator=和/或复制构造函数

Paragraph operator+(const Sentence& sent);

This declares an operator such that adding two Sentence s results in a Paragraph .这声明了一个运算符,以便将两个Sentence相加产生一个Paragraph

 paragraph.sentence = this->sentence + paraSentence;

The right part of the assignment uses the operator from above, so you are trying to assing a Paragraph to a Sentence , as if you had written:赋值的右侧部分使用了上面的运算符,因此您尝试将Paragraph分配给Sentence ,就像您编写的一样:

Paragraph additionResult = this->sentence + paraSentence;
paragraph.sentence = additionResult;

The problem is that you have not defined an assignment from Paragraph in Sentence .问题是您没有在Sentence定义Paragraph的赋值。 You could just add it to Sentence , of course:当然,您可以将其添加到Sentence

Sentence& operator=(const Paragraph& para);

But how would you implement it?但你将如何实施它? Can a paragraph logically be converted into a single sentence?一个段落可以从逻辑上转换成一个句子吗? This solution will not really work.这个解决方案不会真正起作用。

An alternative solution is to change the corresponding operator+ in Sentence to return a Sentence rather than a paragraph:另一种解决方案是更改Sentence相应的operator+以返回Sentence而不是段落:

class Sentence {
    public:
        Sentence();     
        ~Sentence();        
        void show();
        Sentence operator+(const Sentence& sent); // <-- now returns a Sentence
        Paragraph operator+(const Paragraph& paragraph);
        Sentence operator+(const Word& word);

        Word word;              

};

When adding two Sentence s returns a Sentence , then the result of the addition can also be assigned to a Sentence , because copy assignment from the same type is automatically generated by the compiler (unless you explicitly delete it).当两个Sentence相加返回一个Sentence ,那么相加的结果也可以赋值给一个Sentence ,因为相同类型的复制赋值是由编译器自动生成的(除非你明确delete它)。

But this presents its own share of problems, because how can two sentences logically be combined into one?但这也带来了一些问题,因为如何在逻辑上将两个句子合二为一?

The real problem can probably be found in this line:真正的问题可能可以在这一行中找到:

Sentence sentence;      // Sentence in Paragraph

Your class definition effectively says that a paragraph always consists of exactly one sentence.你的类定义有效地表明一个段落总是由一个句子组成。 This cannot be correct.这不可能是正确的。 The member variable should be of type std::vector<Sentence> to express the intention that one paragraph consists of 0 to n sentences.成员变量的类型应该是std::vector<Sentence>以表达一个段落由0 到 n 个句子组成的意图。 Once you have changed the member variable, rewrite all operator implementations to account for the new situation.更改成员变量后,请重写所有运算符实现以适应新情况。

Of course, you have the same problem in Sentence (and I guess in your other classes, too).当然,你在Sentence也有同样的问题(我猜在你的其他课程中也是如此)。


Generally, check your books/tutorials again and review the chapter about operator overloading.通常,再次检查您的书籍/教程并查看有关运算符重载的章节。 You are not following best practices.您没有遵循最佳实践。 For example, you should define + in terms of += .例如,您应该根据+=定义+ And of course, an important question is whether operator overloading is actually helpful here or not.当然,一个重要的问题是运算符重载在这里是否真的有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM