I'm serious stuck and really need some help. I'm working on a calorie counter. It's a simple program that reads ingredients from a nutrient file and a recipe file. The nutrient file fields are name, amount, units, and calories. The recipe file fields are actual amount, units, and name. Not every ingredient in the nutrient file is used in the recipe. If both the name and unit matches, then it will print "total number of calories in one serving ..." If only some match, it will print "at least...."
The problem I'm having is with the search function. I'm trying to do a seq search using a function template to match the names and units but I keep getting a no match function for call to error. Deduction conflicting types for parameter 'T'. I take it 'T' is no reading that I'm trying to pass a string. I've tried removing T and put in string name but I still get the same error. I would appreciate it if someone will tell me what I'm missing. I've been working on this off and on for a while now.
#ifndef VECTORUTILS_H
#define VECTORUTILS_H
#include <vector>
// Search a vector for a given value, returning the index where
// found or -1 if not found.
template <typename T>
int seqSearch(const std::vector<T>& list, T searchItem)
{
int loc;
for (loc = 0; loc < list.size(); loc++)
if (list[loc] == searchItem)
return loc;
return -1;
}
// Search an ordered vector for a given value, returning the index where
// found or -1 if not found.
template <typename T>
int seqOrderedSearch(const std::vector<T> list, T searchItem)
{
int loc = 0;
while (loc < list.size() && list[loc] < searchItem)
{
++loc;
}
if (loc < list.size() && list[loc] == searchItem)
return loc;
elsereturn -1;
}
if you pass a vector of Nutriant to seqOrderedSearch the second parameter has to be a Nutriant ie seqOrderedSearch(std::vector<Nutriant> , Nutriant);
you are doing seqOrderedSearch(std::vector<Nutriant> , std::string);
also your computeCalories function is missing a closing brace.