C++ 将int转换为泛型模板类型

C++ 将int转换为泛型模板类型,c++,C++,我在TreeWrapper.h中有以下函数: template<typename Key, typename Data, class Compare=std::less<Key>> class TreeWrapper{ // ............. public: void addNode(Key &key, Data &data) { currentAVLTree.addNode(key, data); } template<type

我在
TreeWrapper.h
中有以下函数:

template<typename Key, typename Data, class Compare=std::less<Key>>
class TreeWrapper{
// .............
public:
void addNode(Key &key, Data &data) {
    currentAVLTree.addNode(key, data);
}
template<typename Key, typename Data, class Compare=std::less<Key>>
class AVLTree {
    Compare keyCompare;
    AVLNode<Key, Data> *root;
    int length;

public:

    bool addNode(Key &key, Data &data) {
        // impl
    }
};
但是
getTotal
返回
int
而不是
Data
。是否有可能以某种方式转换它?我认为,
数据
是一种获得anytype的泛型类型。为什么它不起作用

编辑

TreeWrapper.h
中的重要部分:

template<typename Key, typename Data, class Compare=std::less<Key>>
class TreeWrapper {
    AVLTree<Key, Data, Compare> currentAVLTree;

public:
    void addNode(Key &key, Data &data) {
        currentAVLTree.addNode(key, data);
    }
};
GroupWrapper
中:

class GroupWrapper {
private:
     TreeWrapper<Group, Group, compareGroup> currentGroup;
public:
    void addGroup(Group group) {
         currentGroup.addNode(group, group.getTotal());
    }
};
class-GroupWrapper{
私人:
树状组;
公众:
无效添加组(组){
currentGroup.addNode(group,group.getTotal());
}
};

但我得到一个错误:
参数类型不匹配:表达式必须是
group.getTotal()
上的左值,它建议将
数据
更改为
int
。为什么?

您传递的是非常量引用,这就是它抱怨左值的原因,左值表示可以位于赋值左侧的值。如果要将函数调用放在左侧,则函数调用需要返回一个引用,而函数调用只返回纯int

更改getTotal以返回引用可能是一个非常糟糕的主意,因此还有两种解决方案

至少为数据添加常量:

bool addNode(Key &key, const Data &data) 
或传递一个值:

bool addNode(Key &key, Data data) 

考虑对键进行相同的更改。

您传递的是非常量引用,这就是它抱怨左值的原因,左值表示可以位于赋值左侧的值。如果要将函数调用放在左侧,则函数调用需要返回一个引用,而函数调用只返回纯int

更改getTotal以返回引用可能是一个非常糟糕的主意,因此还有两种解决方案

至少为数据添加常量:

bool addNode(Key &key, const Data &data) 
或传递一个值:

bool addNode(Key &key, Data data) 
考虑对键进行相同的更改

我认为,
数据
是一种获得anytype的泛型类型

不完全是。确实,
Data
表示泛型类型,但该视图仅在定义模板时有效。使用模板时,将对其进行实例化。实例化的一部分涉及为通用模板参数提供具体替换。对于该实例化,替换是固定的,不再是通用的

例如,查看
TreeWrapper
模板的声明

template<typename Key, typename Data, class Compare=std::less<Key>>
class TreeWrapper
您可以尝试调用
currentGroup.addNode(group,group.getTotal())
,但由于
getTotal()
返回一个
int
(不能转换为
组&
),签名不匹配

此调用的一个解决方案是建议更改
数据的具体替换。如果你要用

TreeWrapper<Group, int, compareGroup> currentGroup;
currentGroup.addNode(group,group.getTotal())
的调用几乎是*匹配的。但是,这可能会搞乱其他东西,除非您决定使用
Group
作为树的数据类型,而不了解树将保存什么类型的数据


*“几乎”,因为这会将临时引用绑定到非常量引用。由于
addNode
听起来好像它没有必要更改它的任何一个参数,因此它们应该是常量引用(或非引用值),这样可以使调用正常工作

void addNode(const Key &key, const Data &data)
这应该在
TreeWrapper
AVLTree
中完成

我认为,
数据
是一种获得anytype的泛型类型

不完全是。确实,
Data
表示泛型类型,但该视图仅在定义模板时有效。使用模板时,将对其进行实例化。实例化的一部分涉及为通用模板参数提供具体替换。对于该实例化,替换是固定的,不再是通用的

例如,查看
TreeWrapper
模板的声明

template<typename Key, typename Data, class Compare=std::less<Key>>
class TreeWrapper
您可以尝试调用
currentGroup.addNode(group,group.getTotal())
,但由于
getTotal()
返回一个
int
(不能转换为
组&
),签名不匹配

此调用的一个解决方案是建议更改
数据的具体替换。如果你要用

TreeWrapper<Group, int, compareGroup> currentGroup;
currentGroup.addNode(group,group.getTotal())
的调用几乎是*匹配的。但是,这可能会搞乱其他东西,除非您决定使用
Group
作为树的数据类型,而不了解树将保存什么类型的数据


*“几乎”,因为这会将临时引用绑定到非常量引用。由于
addNode
听起来好像它没有必要更改它的任何一个参数,因此它们应该是常量引用(或非引用值),这样可以使调用正常工作

void addNode(const Key &key, const Data &data)

这应该在
TreeWrapper
AVLTree

中完成,我们无法从您显示的代码中分辨出来<代码>数据
可以是类、typedef或模板参数。在你的问题中加入一个。什么是
obj
?请提供一份报告
TreeWrapper
只是一个模板,你需要实例化它来创建一个实例,但只有你知道isif group实际上是group,group::getTotal()返回int,那么你最好声明你的实例TreeWrapper obj;虽然这并不是查找那些int数据点的好方法。似乎您可以调用getTotal()并完全跳过树。我只想将
int
设置到
数据中。为什么不可能?我们无法从您显示的代码中判断<代码>数据
可以是类、typedef或模板参数。在你的问题中加入一个。什么是
obj
?请提供一份报告
TreeWrapper
只是一个模板,你需要实例化它来创建一个实例,但只有你知道isif group实际上是group,group::getTotal()返回int,那么你最好声明你的实例TreeWrapper obj;虽然这对lo来说不是一把好钥匙