你能把话务员+的两边换一下吗
Can you switch the sides of operator+
所以基本上我有一个类"Sentence",#包括"Word"。
句子是单词的链接列表
这是我的问题
"Word+Sentence返回一个在开头添加单词的新句子"所以基本上
Word w = "The";
Sentence s = "dog jumped high."
//the object type of w+s should be a sentence
然而,我得到了错误,
'Sentence' does not name a type
//this is in reference to the return type of overloaded operator+ function, which is in the word class
有没有一种方法可以翻转运算符+重载的右手边和左手边,这样我就可以把代码放在句子类中。
我不能把代码放在句子类中,因为我需要的地方有一个单独的重载函数
s+w
返回一个单词添加到末尾的句子
在C++中,运算符根本不必是成员。因此,只需定义类之外的运算符:
Sentence operator+(const Word &word, const Sentence &sentence);
还要注意,您可以转发声明类:
class Sentence; // forward declaration
class Word {
Sentence operator+(const Sentence &sentence) const;
};
class Sentence {
...
};
// Now that Sentence is defined (not just declared),
// you can define operator+ for Word (instead of just declaring it)
Sentence Word::operator+(const Sentence &sentence) const {
...
}
相关文章:
- 有人能分解一下这个c++模板的语法吗
- 有人能解释一下为什么下界是这样工作的吗C++的
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 有人能为我解释一下C++代码吗
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 你能解释一下什么运行时错误是如何解决它的吗?
- 有人可以解释一下这段代码如何能够反转字符串
- 解释一下 for (char c : str) 的作用?
- 有人可以解释一下这个矩阵幂函数是如何工作的吗?
- 有人可以向我解释一下这段代码的作用吗?
- 任何人都可以解释一下我是否需要 & 在第一个代码片段中
- 我不完全确定此代码的行为.有人可以向我解释一下吗?
- 有人可以解释一下吗?这是关于数组和数组内部 if 语句的一些处理
- 请描述一下在 c++ 中在此类中定义构造函数的方式?
- 我无法理解这一点,有人可以帮我解释一下吗?
- 有人可以解释一下复制功能的作用吗?
- 解释一下这个排序算法是如何工作的?
- 我在 2D 数组的动态内存分配中遇到了一些奇怪的代码C++? 请解释一下这是什么?
- 有人能给我解释一下下面的模板代码吗
- 你能把话务员+的两边换一下吗