与使用运算符重载 C++ 的运算符<<输入输出不匹配
no match for operator << in out using operator overloading c++
我在尝试使用运算符重载时遇到了这些错误。问题是这些文件是我的教练给我的,我知道它们是正确的,但当我试图使用它们时,我会遇到不应该发生的错误。我也无法修改这些文件,所以也许这是我的makefile或我如何调用它,但我无法弄清楚。
Node.cpp:在函数'std::ostream&运算符<lt;(std::ostream&,const Node&)':
Node.cpp:88:错误:"operator<"不匹配<'输入输出<lt;"节点[word="'
Node.cpp:86:注意:候选者是:std::ostream&运算符<lt;(std::ostream&,const Node&)
Node.cpp:89:错误:"operator<"不匹配<'输入输出<lt;((const Node*)inNode)->Node::GetFrequency()'
Node.cpp:86:注意:候选者是:std::ostream&运算符<lt;(std::ostream&,const Node&)
std::ostream& operator<<(std::ostream& out, const Node &inNode)
{
out << "Node [word=" << inNode.GetWord() << ", frequency="; //line 88
out << inNode.GetFrequency() << "]" ;
return out;
}
edit:我已经提供了GetFrquency和GetWord,是的,我确信它们是正确的,因为我无法修改这些文件,而其他人已经成功地没有修改这些文件。
//Return the int for frequency
int Node::GetFrequency() const
{
return m_frequency;
}
//Return the string for the word
string Node::GetWord() const
{
return m_word;
}
节点.h
#ifndef NODE_H
#define NODE_H
#include "Util.h" // For some string functions
using namespace std;
class Node{
public:
Node();
Node(string inWord, int frequency);
~Node();
string GetWord() const;
int GetFrequency() const;
void IncrementFrequency();
bool operator<(const Node &RHS) const;
bool operator==(const Node &RHS);
Node operator=(const Node &RHS);
bool operator%(const Node& RHS) const;
friend std::ostream& operator<<(std::ostream& out, const Node &inNode);
private:
std::string m_word; // The word
int m_frequency; // How often the word has appeared.
};
#endif
Node.cpp
#include "Node.h"
using namespace std;
//No parameter constructor for containers
Node::Node(){}
//Full constructor
Node::Node(string inWord, int frequency) : m_word(inWord),
m_frequency(frequency){}
//Destructor
Node::~Node(){}
//Compares this to RHS and returns true if the word is less than
bool Node::operator<(const Node& RHS) const
{
return (this->m_word < RHS.m_word);
}
//Compares this to RHS and returns true if the words are identical
bool Node::operator==(const Node& RHS)
{
return (this->m_word == RHS.m_word);
}
//Deep copy
Node Node::operator=(const Node& RHS)
{
//Be sure we aren't copying over the node
if( this != &RHS )
{
this->m_word = RHS.m_word;
this->m_frequency = RHS.m_frequency;
}
return *this;
}
//Check to see if we have a substring
bool Node::operator%(const Node& RHS) const
{
//We want to ignore case on this check
string text = this->GetWord();
text = Util::Lower(text);
string compared = RHS.GetWord();
compared = Util::Lower(compared);
// If the substring is longer it really isn't a substring
if (text.length() > compared.length()) {return false;}
//Check each character of the substring against the compared string
for (unsigned int i = 0; i < text.length(); i++){
if (text[i] != compared[i]){
//One miss is all it takes to not have a match
return false;
}
}
return true;
}
//Increment the frequency
void Node::IncrementFrequency()
{
m_frequency++;
}
//Formatted output
std::ostream& operator<<(std::ostream& out, const Node &inNode)
{
out << "Node [word=" << inNode.GetWord() << ", frequency=";
out << inNode.GetFrequency() << "]" ;
return out;
}
//Return the int for frequency
int Node::GetFrequency() const
{
return m_frequency;
}
//Return the string for the word
string Node::GetWord() const
{
return m_word;
}
您需要#include <ostream>
。我强烈怀疑您的头文件只包含<iosfwd>
,它声明了std::ostream
类型,但没有声明与其相关的operator<<
成员或函数。
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何显式调用运算符<<
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 过载输出<<用于类的运算符,以打印其中的元组
- 重载运算符<<:此运算符函数的参数太多
- C++继承运算符<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- 为什么字符串流运算符<<擦除原始值
- 关于使用运算符<<为新手提供C++中的模板
- 重载运算符<<输出地址而不是数据成员
- 错误:没有匹配'运算符<<"在'std::cout
- 重载运算符<<用于ostream语法
- 如何为运算符<<
- Qt过载数据流运算符<<
- 我可以使用define调用运算符<<
- 运算符<<(ostream&os,..)用于模板类
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多
- Can运算符<<用于设计我们自己的输出显示方式