如何在两个不同的类之间重载"=="运算符?
How do I overload the '==' operator between two different classes?
我已经阅读了有关如何使用运算符重载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
要么在标头中定义函数,要么不以内联方式声明它们。
相关文章:
- 构造函数和转换运算符之间的重载解析
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 你能帮我了解重载一元运算符和二进制运算符之间的区别吗?
- 将重载构造函数传递给类之间的函数
- C++模板和非模板函数之间的重载解析
- C++ 类的构造函数和函数调用运算符 () 重载之间的歧义
- 函数隐藏和重载之间的区别
- 模板流运算符重载错误:引用初始化无效,与basic_istream和basic_ifstream之间的差异有关
- Int64 和 time_t 之间的 C++ 重载歧义
- 函数重载和双精度和长之间的混淆
- 基类和派生类中的模板成员之间的重载解析
- 如何在两个不同的类之间重载"=="运算符?
- 派生类 插入和提取运算符重载以及基类与派生类之间的强制转换
- 模板专用类之间的构造函数重载
- C++ 中多态性和函数重载之间的区别
- 指针之间的运算符重载
- C++:使用结构或枚举重载构造函数之间的区别
- 重载和对象定向之间的关系
- ostream 插入运算符与其非成员重载之间的关系
- c++中按引用传递和按值传递之间重载歧义的实用解决方案