类错误 C3867 中的 stl 列表

stl list in a class error C3867

本文关键字:stl 列表 中的 C3867 错误      更新时间:2023-10-16

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

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

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

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

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;
}

有人可以帮我吗?谢谢。

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

list<double> inventoryAmount;

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

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

除了begin上缺少()(导致第一个错误 C3867)之外,分配给迭代器时存在类型不匹配 - 列表需要是相同的类型,因此

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

应该纠正问题。

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

你忘了告诉我们那些神秘的错误代码是什么意思以及它们指的是哪些行。首先是

function call missing argument list

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

invenIterAmount = inventoryAmount.begin();
                                       ^^

第二个是

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

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

通常,在询问错误时,请包含确切的错误消息,并准确指出导致错误的代码行,并有足够的代码来提供我们可以用来重现错误的测试用例。然后,我们不必猜测错误与您的代码有何关系。