C++ 中的指针、类和 void*
我得到了斐波那契堆的代码。此代码使用以下函数来比较两个键:
int
cmp(void *x, void *y)
{
int a, b;
a = (int)x;
b = (int)y;
if (a < b)
return -1;
if (a == b)
return 0;
return 1;
}
之所以有效,是因为 KEY 当前是 INT 数字。
我想修改此代码以使用我的名为“Node”的类。这个类已经实现了运算符<。 >、<=、== 和 >=。
我在代码中的适应是:
Node a, b // instead int a, b
a = (Node)x;
b = (Node)y;
但我得到了错误:
dijkstra.cpp:168: error: no matching function for call to 'Node::Node(void*&)'
graph.h:39: note: candidates are: Node::Node()
graph.h:39: note: Node::Node(const Node&)
我也尝试了:
Node a, b // instead int a, b
a = (Node*)x;
b = (Node*)y;
并得到了错误:
dijkstra.cpp:168: error: no match for 'operator=' in 'a = (Node*)x'
graph.h:39: note: candidates are: Node& Node::operator=(const Node&)
我放弃尝试设置值并解决问题如下:
int cmp(void *x, void *y)
{
if ((Node*)x < (Node*)y)
return -1;
if ((Node*)x == (Node*)x)
return 0;
return 1;
}
我想了解我在中做错了什么前面的例子。
提前致谢。
I get a code for Fibonacci Heap. This code uses the follow function to compare two keys:
int
cmp(void *x, void *y)
{
int a, b;
a = (int)x;
b = (int)y;
if (a < b)
return -1;
if (a == b)
return 0;
return 1;
}
This works because the KEY is currently a INT number.
I would like to adapt this code to use my class called "Node". This class have implemented the operator <. >, <=, == and >=.
My adaptation in the code was:
Node a, b // instead int a, b
a = (Node)x;
b = (Node)y;
But I get the error:
dijkstra.cpp:168: error: no matching function for call to 'Node::Node(void*&)'
graph.h:39: note: candidates are: Node::Node()
graph.h:39: note: Node::Node(const Node&)
I tried also:
Node a, b // instead int a, b
a = (Node*)x;
b = (Node*)y;
and get the error:
dijkstra.cpp:168: error: no match for 'operator=' in 'a = (Node*)x'
graph.h:39: note: candidates are: Node& Node::operator=(const Node&)
I give up to try set the value and solve the question as a follow:
int cmp(void *x, void *y)
{
if ((Node*)x < (Node*)y)
return -1;
if ((Node*)x == (Node*)x)
return 0;
return 1;
}
I would like to understand what I am doing wrong in the previous examples.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
将以下行更改
为
您只是将指针从
void*
类型转换为Node*
并尝试将指针分配给类型为Node
的变量>。Change the following lines
to
You were merely type casting the pointers from
void*
toNode*
and trying to assign the pointer to a variable of typeNode
.您说您为 Node 类重载了运算符(<、== 等)。但该函数按原样比较指针。您需要取消引用它们才能使用运算符。试试这个:
You said you overloaded operators(<,==,etc..) for the Node class. But the function compares pointers as is. You need to dereference them in order to use your operators. Try this:
看起来您正在使用对 Node 对象的引用来调用 cmp 函数(或者通过回调调用它)。如果是这种情况,您可以将代码更改为
“我猜”,而无需查看调用代码 - 老实说,这不是一个好主意。如果您也能贴出调用代码,将有助于更好地回答问题。
Looks like you are calling the
cmp
function (or it is getting called via a callback) with a reference to the Node object. If that is the case you can change your code toI am guessing this without looking at the calling code - which, honestly is not a good idea. If you can post the calling code too, it will be helpful to answer the question better.
没有理由比较 (Node*) 或 (void*) 值 - 这只是指针,您将比较内存地址。看起来让第一个示例工作所需的只是实现
operator =
。There no reason to compare (Node*) or (void*) values - this is just pointers and you will compare memory addresses. It looks like all you need to make first example work is implement
operator =
.添加 yasouser 所说的内容,您的第一个程序将无法按预期工作。
Adding to what yasouser said, your first program will not work as expected.
首先关于您的 cmp 函数本身:
通常人们只实现
operator<
;当实现operator>
时,通常是通过使用相反的参数调用已经实现的operator<
。您的 cmp 实现还需要 Node 实现operator==
,这可能是不必要的。另外,如果您的函数接受 void* 但在内部将它们转换为 NODE* 并取消引用它们,并且它仍然以通用“cmp”命名,则会出现一个小问题。
更好的实现是这样的:
如果您愿意,您可能需要研究 std::less 或模板函数。
First about your cmp function itself:
usually people only implement
operator<
; whenoperator>
is implemented, it is usually through a call to the already implementedoperator<
with reversed parameters. Your implementation of cmp also requires the Node implementoperator==
, which might be unnecessary.Also, there is a small issue if your function accepts void* but internally converts them to NODE* and dereferences them AND it yet it is named with a generic "cmp".
A better implementation would be this:
If you're feeling up to it, you might want to look into std::less or template functions.
通常,当您将
void*
传递给函数时,您必须将该指针强制转换为所需类型的另一个指针。例如:另一方面,您在编写
或时
尝试对
T
类型的对象进行值初始化这就是为什么您会收到关于T。
您应该做的是转换为
T*
类型的指针,然后取消引用这些新的转换指针,以在实际对象之间进行任何您想要的比较 类型为T
。例如(假设T
是您想要比较的某种所需类型......即,这不是模板):Typically when you are passing a
void*
to a function, you must then cast the pointer to another pointer of the type you are wanting. For example:You on the otherhand are trying to value initialize an object of type
T
when you writeor
That is why you're getting complaints about not having a proper constructor type for the object of type
T
.What you should be doing is casting to a pointer of type
T*
, and then dereferencing those new casted pointers to-do whatever comparisons you want between the actual objects of typeT
. For example (assumeT
is some desired type you want to compare ... i.e., this is not a template):