Java 什么是整数?
整数规划问题是一个数学优化或可行性程序,其中一些或所有变量被限制为整数。您的Java 什么是整数?,java,Java,整数规划问题是一个数学优化或可行性程序,其中一些或所有变量被限制为整数。您的myintstrict变量被声明为BDictionaryBDictionary的键是int和E是std::string 当您调用myintstrict.removeAny(strData)时,BDictionary::removeAny()方法将E&akastd::string&作为输入,strData是std::string,因此一切正常 在BDictionary内部,其字典成员声明为ABag*ABag的E是KVpai
myintstrict
变量被声明为BDictionary
BDictionary
的键是int
和E
是std::string
当您调用myintstrict.removeAny(strData)
时,BDictionary::removeAny()
方法将E&
akastd::string&
作为输入,strData
是std::string
,因此一切正常
在BDictionary
内部,其字典
成员声明为ABag*
ABag
的E
是KVpair
ABag::removeTop()
因此,当您调用dictionary->removeTop(returnValue)
时,您传递的是一个std::string
,其中需要KVpair
,这就是编译器失败的原因
要执行您正在尝试的操作,您需要修复BDictionary::removeAny()
方法的实现,以接受KVpair
,并且您可以为此提取std::string
,例如:
//BDictionary.h
template <typename Key, typename E>
class BDictionary : public Dictionary<Key, E> {
ABag<KVpair<Key, E>>* dictionary; //Dictionary object
bool removeAny(E& returnValue) { //The Dictionary method that accepts an E& value
KVpair<Key, E> pair;
bool res = dictionary->removeTop(pair);
if (res) returnValue = pair.second; // or however KVpair refers to its 2nd element
return res;
}
};
//BDictionary.h
模板
B类词典:公共词典{
ABag*字典;//字典对象
boolRemoveAny(E&returnValue){//接受E&value的字典方法
KVpair对;
bool res=字典->移除(成对);
if(res)returnValue=pair.second;//或者KVpair引用其第二个元素
返回res;
}
};
在BDictionary::removeAny
中,对ABag::removeTop
的调用想要“返回”一个KVPair
。但是removeAny
仅用于“返回”E
。您不能将removeAny
的E&returnValue
参数传递到removeTop
,因为removeTop
将要返回整对,而不仅仅是值,而且只有值将“适合”在E&
中
在不深入和修改ABag
的内部结构的情况下,我认为最好的方法是在removeAny
中创建一个临时KVPair
“destination”,以保存来自removeTop
的值,然后只传递该对的值部分。这很难看,但它能起作用:
bool removeAny(E &returnValue) {
KVPair<Key, E> dest;
bool removed = dictionary->removeTop(dest);
if(removed) returnValue = std::move(dest.value); // or however you get the value out
return removed;
}
这需要修改您的呼叫代码(在bagtestmain.cpp
中)。您粘贴的错误消息似乎被截断了。你能粘贴完整的编译器输出吗?我用完整的编译器错误更新了帖子。请添加足够的代码,这样我们就可以知道每个上下文中E
是什么。(比如一个模板类{
..};
?)ABag中的E类型是KVpair
,Dictionary中的E类型是int
。编译器会通知您,无法将int
转换为KVpair
A是有帮助的,因为您这里的内容太零碎和复杂,如果我输入缺少的部分以使其进行编译,我很有可能不会输入您遇到的错误。
bool removeAny(KVPair<Key, E> &dest) {
return dictionary->removeTop(dest);
}