与使用运算符重载 C++ 的运算符<<输入输出不匹配

no match for operator << in out using operator overloading c++

本文关键字:运算符 lt 输入输出 不匹配 C++ 重载      更新时间:2023-10-16

我在尝试使用运算符重载时遇到了这些错误。问题是这些文件是我的教练给我的,我知道它们是正确的,但当我试图使用它们时,我会遇到不应该发生的错误。我也无法修改这些文件,所以也许这是我的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<<成员或函数。