我一直收到此错误,无法与'operator==' 'temp2 == temp3'

I keep getting this error no match for 'operator==' in 'temp2 == temp3'

本文关键字:temp3 temp2 operator 一直 错误      更新时间:2023-10-16
#include<iostream>  
#include<cctype>
#include<string>
#include<cstdlib>
#include"Palindrome.h"
using namespace std;
struct Node
{
  string pharse;
  Node* next;
};
int main()
{           
  Stack S1;
  Queue Q1;
  string word;
  int length;
  cout << "Do you know what a Palindrome is?n";
  cout << "It is a word that is the same spelling backwards and forwardn";
  cout << "Enter in a word ";
  getline(cin, word);
  //cout.flush();
  length = word.length();
  string NewWord1[length];
  string NewWord2[length];
  for(int c =0; c < length; c++)
  {
    NewWord1[c] = word[c];
    NewWord2[c] = word[c];
  }
  for(int c =0; c < length; c++)
  {
    cout << NewWord1[c];
    cout << endl;
    cout << NewWord2[c];
    cout << endl;
  }

  cout << "end";
  S1.push(NewWord1, length);
  Q1.enqueue(NewWord2, length); 
  Node temp2 = S1.pop();
  Node temp3 = Q1.dequeue();
  if(temp2 == temp3)
    cout << "They are palindrome.";
  else
    cout << "They are not palindrome.";
  /*S1.pop();*/
  return 0;
}
void Stack :: push(string NewWord1[], int size)
{
        //cout << NewWord1[0];
        if(!isFull())   
        {
                for(int i = 0; i < size; i++)
                {
                        Node *temp = new Node;
                        temp -> pharse = NewWord1[i];
                        temp -> next = head1;
                        head1 = temp;
                }
        }
}   

//pop和enqueue返回一个Node,它是我的结构

由于Nodes是对象,您必须重载operator==以使该类利用该语法。请显示Node的类声明,以便我们可以帮助您实现(它可能像比较两个节点的数据成员一样简单)。定义为

bool Node::operator==(const Node & other) const
{
    return this->phrase == other.phrase;
}

请参阅Vink的文章,因为这不是问题的根源。您需要字符串,而不是字符串数组,并且您需要将字符压入堆栈。

这能编译吗?

NewWord1和NewWord2是字符串数组,而不是字符串。

节点是否包含字符、字符串或字符串数组?

我猜Node没有定义bool operator==(Node const&) const