我正在研究一个项目,该项目读取食谱并将其与食品室中的内容(用户定义)进行比较。
我希望通过在线搜索减少大部分问题,但其中一个特别烦人。
每当我设置迭代器时 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;
}
有人可以帮我这个吗?谢谢。
答案 0 :(得分:0)
您忘了告诉我们这些神秘的错误代码是什么意思以及它们引用的是哪些行。第一个是
function call missing argument list
因为您忘记在调用()
和begin
函数后放置end
:
invenIterAmount = inventoryAmount.begin();
^^
第二个是
no operator defined which takes a left-hand operand of type 'type'
意味着您将某种运算符应用于未定义该运算符的类型。我猜测来自recipeIter != recipeFoodName.end
之类的比较,当您添加缺失的()
时,这些比较将得到修复。
通常,在询问错误时,请包含确切的错误消息,并准确指出导致它的代码行,只需要足够的代码来提供我们可以用来重现错误的测试用例。然后我们不必猜测错误与代码的关系。
答案 1 :(得分:0)
在您的班级定义中,您已将列表声明为double
的列表,例如
list<double> inventoryAmount;
然后在extractData::compareInventoryRecipe()
中为int
列表创建一个迭代器:
list<int>::iterator invenIterAmount;
invenIterAmount = inventoryAmount.begin;
除了()
上缺少begin
(这会导致您的第一个错误C3867),在分配给迭代器时,您的类型不匹配 - 列表需要相同类型,所以
list<double>::iterator invenIterAmount;
invenIterAmount = inventoryAmount.begin();
应该纠正这个问题。
您的其他一些列表存在类似的问题,例如: recipeAmount
。