什么是整数?

问题描述 投票:0回答:2

整数编程问题是一个数学优化或可行性程序,其中一些或所有的变量被限制为整数。

java
2个回答
0
投票

您的 myIntStrDict 变量被声明为 BDictionary<int, std::string>. BDictionary's KeyintEstd::string.

当你打电话 myIntStrDict.removeAny(strData)జజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ BDictionary::removeAny() 方法取一个 E& 又名 std::string& 作为输入,并 strData 是一个 std::string,所以一切都好。

里面的 BDictionary,其 dictionary 成员被宣布为 ABag<KVpair<Key, E>>*. ABag's EKVpair<Key, E>.

ABag::removeTop() 方法取一个 E& 又名 KVpair<Key, E>& 输入,而不是 std::string& 就像你所期待的那样。

所以,当你打电话 dictionary->removeTop(returnValue),你正在通过一个 std::string 其中a KVpair 是预期的,这就是编译器失败的原因。

要做你正在尝试的事情,你需要修复你的 BDictionary::removeAny() 方法来接受一个 KVpair,和nyou可以提取的 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;
    }
};

0
投票

BDictionary<E>::removeAny呼叫:"我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊,我的天啊。ABag<KVPair<Key, E>>::removeTop 要 "还 "一个 KVPair<Key, E>. 但是... removeAny 只具备 "返回 "一个 E. 你不能通过 E &returnValue 的论点 removeAnyremoveTop,因为 removeTop 会希望返回整个对子,而不仅仅是值,但只有值才会 "适合 "在 E&.

在此不赘述,也不修改内部的 ABag我想你能做的最好的是创建一个临时的 KVPair<Key, E> "目的地 "中 removeAny 以保持来自 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;
}

或者,修改一下 removeAny 这样,它也会返回整个对。

bool removeAny(KVPair<Key, E> &dest) {
    return dictionary->removeTop(dest);
}

这将需要修改你的调用代码(在 bagtestmain.cpp).

© www.soinside.com 2019 - 2024. All rights reserved.