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

时间:2019-09-23 17:55:04

标签: c++ c++11 operator-overloading

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

我有一个node结构,根据Geeks for Geeks教程,该结构由Node(包装类)包装,似乎我想说{{1} },那么我必须定义一个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

有人可以帮助我了解我在做什么错吗?

1 个答案:

答案 0 :(得分:2)

  

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

典型的解决方案是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

要么在头文件中定义函数,要么不内联声明它们。