Я получаю эти ошибки при попытке использовать перегрузку оператора. Проблема в том, что эти файлы были предоставлены мне моим инструктором, и я знаю, что они верны, но когда я пытаюсь их использовать, я получаю ошибки, которых не должно было случиться. Я также не могу изменить эти файлы, поэтому, возможно, это мой make-файл или как я его называю, но я не могу понять это.нет соответствия для оператора << при использовании перегрузки оператора C++
node.cpp: В функции 'станд :: ostream & оператор < < (станд :: ostream &, Const Node &)':
node.cpp: 88: ошибка: не подходит для «оператора < < 'в 'из < < "Node [слово ="'
node.cpp: 86: примечание: кандидаты: станд :: ostream & оператор < < (станд :: ostream &, Const Node &)
node.cpp: 89: ошибка: не подходит для 'оператора < <' в 'из < < ((Const Node *) inNode) -> Node :: GetFrequency()'
Node. каст: 86: Примечание: кандидаты: станд :: ostream & оператор < < (станд :: ostream &, Const Узел &)
std::ostream& operator<<(std::ostream& out, const Node &inNode)
{
out << "Node [word=" << inNode.GetWord() << ", frequency="; //line 88
out << inNode.GetFrequency() << "]" ;
return out;
}
редактировать: Я представил 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;
}
node.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;
}
Вы также указали переадресацию (друга) для этой перегрузки оператора? –
_ «Я знаю, что они верны, но когда я пытаюсь их использовать, я получаю ошибки, которые не должны происходить». _ Тогда, очевидно, они не соответствуют правилу ... –
Покажите нам свой тестовый файл. Заголовки. Типы. Все это важно. –