如何在两个不同的类之间重载"=="运算符?

How do I overload the '==' operator between two different classes?

本文关键字:之间 重载 运算符 两个      更新时间:2023-10-16

我已经阅读了有关如何使用运算符重载C++但仍然感到困惑的教程。

我有一个node结构,根据极客的极客教程,它由Node(包装类(包装,似乎如果我想能够说n == N那么我必须定义一个global运算符,并使其成为该类的朋友?我这样做了,但它仍然不起作用,我收到此错误

g++    -c -o main.o main.cpp
In file included from main.cpp:3:0:
node.hpp:25:29: warning: inline function ‘virtual bool Node::operator==(const Node&)’ used but never defined
virtual inline bool operator==(const Node &n);
^~~~~~~~
node.hpp:27:28: warning: inline function ‘bool operator==(node, const Node&)’ used but never defined
friend inline bool operator==(const node n, const Node &nt);
^~~~~~~~
node.hpp:26:29: warning: inline function ‘virtual bool Node::operator==(node)’ used but never defined
virtual inline bool operator==(const node n);
^~~~~~~~
g++ -o edit main.o node.o
main.o: In function `main':
main.cpp:(.text+0x128): undefined reference to `operator==(_node_str*, Node const&)'
collect2: error: ld returned 1 exit status
Makefile:5: recipe for target 'edit' failed
make: *** [edit] Error 1

这对我来说毫无意义,因为我在node.cpp中定义了==函数,所以据我所知它并不是未定义的

这是我的代码:

node.hpp

#pragma once
#include <iostream>
typedef unsigned long int reference_counter;
typedef struct _node_str {
reference_counter ref_cnt;
} node_str;
typedef node_str* node;
class Node {
private:
friend std::ostream& operator<<(std::ostream&, const Node&);
public:
node n;
Node();
Node(node n);
virtual ~Node();
virtual inline node operator->();
virtual inline bool operator==(const Node &n);
virtual inline bool operator==(const node n);
friend inline bool operator==(const node n, const Node &nt);
};
node node_create();

然后在node.cpp

#include "node.hpp"
node node_create()
{
node temp = (node)malloc(sizeof(node_str));
temp->ref_cnt = 0;
return temp;
}
// Node class definitions
Node::Node()
{
this->n = nullptr;
}
Node::Node(node n)
{
this->n = n;
this->n->ref_cnt++;
}
Node::~Node()
{
this->n->ref_cnt--;
// TODO possibly check for and delete node upon count reaching 0
}
node Node::operator->()
{
return this->n;
}
inline bool Node::operator==(const Node &n)
{
return this->n == n.n;
}
inline bool Node::operator==(const node n)
{
return this->n == n;
}
inline bool operator==(const node n,const Node &nt)
{
return n == nt.n;
}
std::ostream& operator<<(std::ostream &strm, const Node &a)
{
return strm << "Node Wrapper { " << a.n << ", Count: " << a.n->ref_cnt << " }";
}

然后main.cpp一个简单的测试来使用等价。

#include <iostream>
#include "node.hpp"
using namespace std;
int main()
{
cout << "Hello World!" << endl;
node a = node_create();
node b = node_create();
Node A = Node(a);
Node B = Node(b);
cout << A << endl << B << endl;
cout << ((A == B) ? "A == B" : "A != B") << endl;
cout << ((a == A) ? "a == A" : "a != A") << endl;
cout << ((A == a) ? "A == a" : "A != a") << endl;
cout << A << endl;
}

以及将它们包裹在一起makefile

CC = g++
objects = main.o node.o
edit : $(objects)
$(CC) -o edit $(objects)
main.o : main.cpp node.hpp
node.o : node.cpp node.hpp
.PHONY : clean
clean :
rm edit $(objects)

有人能帮助我理解我在这里做错了什么吗?

如何在两个不同的类之间重载"=="运算符?

典型的解决方案是 1.使一种类型隐式转换为另一种类型,并使 2.使用非成员重载使公共类型与自身具有可比性。它可能不是成员重载,因为这不允许隐式转换左操作数。

您的尝试也有效,但您必须在三次不同的时间实现相同的比较。我上面的建议适用于单个重载。


我收到此错误

undefined reference to `operator==(_node_str*, Node const&)'

问题是您在内联中声明了该函数,但未能在调用该函数的所有翻译单元中定义该函数。特别是,您只在node.cpp中定义了它们,但在main.cpp中调用它们。编译器警告不要这样做:

warning: inline function ‘virtual bool Node::operator==(const Node&)’
used but never defined

要么在标头中定义函数,要么不以内联方式声明它们。