- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
error: no matching function for call to ‘BSTreeNode::BSTreeNode(int, int, NULL, NULL)’
candidates are: BSTreeNode::BSTreeNode(KF, DT&, BSTreeNode*, BSTreeNode*) [with KF = int, DT = int]
我是这样用的:
BSTreeNode<int, int> newNode(5,9, NULL, NULL) ;
我是这样定义的:
BSTreeNode(KF sKey, DT &data, BSTreeNode *lt, BSTreeNode *rt):key(sKey),dataItem(data), left(lt), right(rt){}
以这种方式使用我的构造函数有什么问题?
我整晚都在拔头发,请尽快帮帮我!!
最佳答案
非常量引用不能绑定(bind)到右值,这就是您要对 DT &data
参数的参数 9
所做的。
您要么需要传入一个值为 9
的变量,要么您需要更改参数(以及 dataItem
成员,如果它是reference) 为 DT
类型,按值复制到对象中。即使您将引用更改为 const
以消除编译器错误,如果传入的参数是临时的(它不会超过构造函数调用,所以你会留下一个悬垂的引用)。
这是一个小示例程序,演示了将对象中的 const 引用绑定(bind)到临时对象(从 rand()
返回的 int 值)时出现的问题。请注意,该行为是未定义的,因此它可能在某些条件下看起来有效。我在 MSVC 2008 和 MinGW 4.5.1 上使用调试版本测试了这个程序:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
template <class KF, class DT>
class BSTreeNode {
private:
KF key;
DT const& dataItem;
BSTreeNode* left;
BSTreeNode* right;
public:
BSTreeNode(KF sKey, DT const &data, BSTreeNode *lt, BSTreeNode *rt)
: key(sKey)
, dataItem(data)
, left(lt)
, right(rt)
{}
void foo() {
printf( "BSTreeNode::dataItem == %d\n", dataItem);
}
};
BSTreeNode<int, int>* test1()
{
BSTreeNode<int, int>* p = new BSTreeNode<int, int>(5,rand(), NULL, NULL);
// note: at this point the reference to whatever `rand()` returned in the
// above constructor is no longer valid
return p;
}
int main()
{
BSTreeNode<int, int>* p1 = test1();
p1->foo();
printf( "some other random number: %d\n", rand());
p1->foo();
}
运行示例显示:
BSTreeNode::dataItem == 41
some other random number: 18467
BSTreeNode::dataItem == 2293724
关于c++ - 错误 : no matching function for call to ‘BSTreeNode<int, int>::BSTreeNode(int, int, NULL, NULL)’ - what's wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4529508/
这个问题在这里已经有了答案: java.lang.IllegalArgumentException: The servlets named [X] and [Y] are both mapped t
我无法让我的 WebRTC 代码正常工作。我相信我所做的一切都是正确的,但它仍然无法正常工作。为什么 ontrack 这么早就被调用有些奇怪,也许它应该是那样的。 该网站使用 javascript 代
使用 Mac OSX 优胜美地 (10.10.4): rails -v => Rails 4.2.3 ruby -v => ruby 2.2.2p95 遵循这些说明的组合: https://www
您好,我正在尝试使用(缓存的)已编译的 lambda 表达式来访问属性,与使用 PropertyInfo.GetValue()/SetValue() 方法调用相比,我确实得到了更好(即更快)的结果。然
我编写此代码是为了获取学生的字母成绩并计算他们的 GPA。当我运行该程序时,我可以正确获取学生的姓名和科目,但无法显示成绩或 GPA。 示例输入: Sally 1 A N 示例输出: Enter St
我一直在编写这段代码,根据这本书应该可以做到这一点: Write a script that creates and calls a stored procedure named test. This
我真的很难创建一个具有以下基本格式的有效多维 JavaScript 数组: var countries = [ { "country": "UK", "properties": {
我有一个小型 Python OOP 程序,其中 2 个类 Flan 和 Outil 继承自父类(super class) Part。 我的问题是,当我调用 Flan 时一切正常,但是当我调用 Outi
我目前正在尝试使用通用监听器来编写事件系统。 所有监听器都应添加到单个 EventSource 对象,该对象将为特定事件调用其 receiveEvent() 方法。 事件源: public class
我正在通过我的 PHP 应用程序发送电子邮件。但是,它们被 Gmail 标记为垃圾邮件。这是我发送电子邮件的方式(PHP): $headers = "From: test@bookmytakeout.
我已经正式走到了穷途末路的地步。我找不到我做错了什么。我完成的这个程序几乎与我几天前编写的另一个程序一模一样,但我在编译时遇到了问题。我不知道为什么输出线上出现错误。请帮忙: 这是正在运行的文件: p
---编辑:我不允许使用任何包或预置方法。不用担心,我不想让你做我的“作业”,我只需要一点提示!---我发现these interesting Algorithms 。我想使用按位添加方法。我的问题是
我制作了一个小程序,尝试使用 conn.getOutputStream(); 检索 URLConnection 对象输出流。当我尝试执行此操作时,我的小程序抛出异常 java.net.UnknownS
每当我尝试在 SVN 中合并时,我都会遇到成堆的树冲突。好吧,就此示例脚本而言,只有一个,但仍然如此。 #!/bin/bash svnadmin create repo svn checkout fi
我开始为 Scala 中的 X500PrincipalBuilder 类编写单元测试。这是我的测试代码: import org.junit.runner.RunWith import org.scal
我正在用 python 编写我的第一个程序,它必须模拟粒子(两种气体)的混合。我不知道我这个功能做错了什么。我不希望颗粒离开某些区域,即容器的壁。我使用 VPython。 def poruszanie
我正在尝试求解三角方程组,但我认为 Python 没有生成正确的解。我试图解决的方程: 1 − 2cosθ1 + 2cosθ2 − 2cosθ3 = −0.8 1 − 2cos5θ1 + 2cos5θ
这个问题已经有答案了: TypeError: worker() takes 0 positional arguments but 1 was given [duplicate] (11 个回答) 已关
大家好,我正在努力解决这个问题 编写一个 C 程序,计算弹丸在撞击地面之前行进的距离(即射程)、弹丸撞击地面所需的时间以及弹丸飞行中的最大高度(给定角度)它被射向空中,以及发射时的初始速度(速度)。我
我编写了代码来计算 QuickSort 中完成的比较次数。 每当对长度为 m 的数组执行快速排序时,该算法都会将比较次数增加 m-1(因为主元将与除自身以外的所有内容进行比较)。 枢轴的选择始终是数组
我是一名优秀的程序员,十分优秀!