【问题标题】:stl list in a class error C3867stl 列表中的类错误 C3867
【发布时间】:2014-11-29 10:59:48
【问题描述】:

我正在开展一个项目,该项目读取食谱并将其与您的储藏室中的食物(用户定义)进行比较。

我希望通过在线搜索减少了大部分问题,但有一个问题特别烦人。

每当我设置一个迭代器 list::iterator listThatIsInClass;等于开始或结束,Visual Studio Express 2013 给了我一个 C3867 错误。执行逻辑运算时也会出现C2678错误,小于等。

下面是一个不起作用的功能

void extractData::compareInventoryRecipe()

{
    string recipIng = " ";
    string invenIng = " ";
    double recipAm = 0;
    double invenAm = 0;
    double x = 0;
    double y = 0;
    list<int>::iterator invenIterAmount;
    invenIterAmount = inventoryAmount.begin;
    list<int>::iterator recipeIterAmount;
    recipeIterAmount = recipeAmount.begin;

for (list<int>::iterator recipeIter = recipeFoodName.begin; recipeIter != recipeFoodName.end; ++recipeIter)
{
    ++recipeIterAmount;
    for (list<int>::iterator invenIter = inventoryItem.begin; invenIter != inventoryItem.end; ++invenIter)
    {
        ++invenIterAmount;
        recipIng = *recipeIter;
        invenIng = *invenIter;
        recipAm = *recipeIterAmount;
        invenAm = *invenIterAmount;
        if (recipIng.compare(invenIng) == 0)
        {
            if (invenAm < recipAm)
            {
                x += (invenAm / recipAm);
                neededAmount.push_back(invenAm - recipAm);
                neededItem.push_back(invenIng);
            }
            else
            {
                x += 1;
                recipeFoodName.remove(invenIng);
                recipeAmount.remove(invenAm);
            }
            break;
        }
        else
            continue;
    }
    y += 1;
}
setPercentOnHand((x * 100) / y);

}

下面是头文件。如您所见,所有列表都处于受保护状态,因为之前存在一个问题,即函数无法私下访问它们。

class extractData
{
    protected:
      list<string> recipeMeasurmentType
      list<string> recipeFoodName;
      list<double> recipeAmount;

      list<string> inventoryItem;
      list<double> inventoryAmount; // do i need regular int and string as well?

      list<string> neededItem;
      list<double> neededAmount;
      list<string> measurmentLetter;
private:
    string recipeTitle;
    int choice;
    double percentOnHand;

我确实尝试过制作一个包含迭代器的模板(我对此不是很有经验)。下面是模板,我不知道如何处理它。它是头文件中的公共类。

template <class T>
void iterators(list<T> data)
{
    typename list<T>::iterator begin();
    typename list<T>::iterator end();

    typename list<T>::const_iterator begin() const;
    typename list<T>::const_iterator end() const;
}

有人可以帮我解决这个问题吗?谢谢。

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建最小、完整和可验证的示例。
  • std::vector 应该是默认容器,除非您有理由选择其他内容。

标签: c++ list templates visual-studio-2013 stl


【解决方案1】:

在您的类定义中,您已将列表声明为 double 列表,例如

list<double> inventoryAmount;

然后在extractData::compareInventoryRecipe() 中为int 列表创建一个迭代器:

list<int>::iterator invenIterAmount;
invenIterAmount = inventoryAmount.begin;

除了begin 上缺少()(这会导致您的第一个错误C3867)之外,您在分配给迭代器时存在类型不匹配 - 列表必须是相同的类型,所以

list<double>::iterator invenIterAmount;
invenIterAmount = inventoryAmount.begin();

应该更正问题。

您的其他一些列表也存在类似问题,例如recipeAmount.

【讨论】:

  • 我明白了。我认为迭代器必须是一个int。我会尝试更新结果。
【解决方案2】:

您忘记告诉我们这些神秘的错误代码是什么意思以及它们所指的行。第一个是

function call missing argument list

因为您忘记在对beginend 函数的各种调用之后加上()

invenIterAmount = inventoryAmount.begin();
                                       ^^

第二个是

no operator defined which takes a left-hand operand of type 'type'

意味着您将某种运算符应用于未定义该运算符的类型。我猜这来自recipeIter != recipeFoodName.end 之类的比较,当您添加缺少的() 时,这将得到修复。

一般来说,当询问错误时,请包含确切的错误消息,并准确指出是哪一行代码导致了它,并提供足够的代码来提供一个测试用例,我们可以使用它来重现错误。这样我们就不必猜测错误与您的代码有何关系。

【讨论】:

    猜你喜欢
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2019-10-11
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多