0

I'm making my first use of c++ template to declare a simple returning for a vector of ints, int64_t, floats or doubles its mean and standard deviation in a pair of doubles objects.

I've been trying to copy the models I found on the internet and started with:

template<typename Type>
pair<double,double> mean_std(vector<Type>::iterator the_beg, vector<Type>::iterator the_end){
// rest of my function

but this already returns me the errors:

 template declaration of ‘std::pair<double, double> mean_std’
 code.cpp:22:53: error: expected ‘)’ before ‘the_beg’
 code.cpp:22:85: error: expected ‘)’ before ‘the_end’

when compiling.

Is it because of some subtlety in using templates with vector (or their iterators)?

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • 4
    Use the `typename` keyword with both parameters (right before `vector`). Also, are you `using namespace std`? I would actually recommend against doing this, but the way you've written the code requires it. – dlf Oct 27 '14 at 14:45
  • 4
    `typename vector::iterator the_beg` welcome to templates. – Neil Kirk Oct 27 '14 at 14:45
  • What happens if the average etc is not representable for Type by a double? Eg complex number (don't know how that works with averages, not a maths pro). Also you should create two different functions to calculation mean and standard deviation. – Neil Kirk Oct 27 '14 at 14:46
  • @NeilKirk : Many thanks!! Indeed it works after adding typename. Answering your next points: I have no intentions, for now, on using it on complex numbers, and I prefer extracting the mean and std in one pass, as I always need both of them. – Learning is a mess Oct 27 '14 at 14:50
  • @dlf: Yes I declared `using namespace std;` above these lines of code, indeed otherwise I would have `std::vector`. I know there are points for which it can become a problem but so far so good in my case (I mean no conflicts between names in the std and elsewhere) – Learning is a mess Oct 27 '14 at 14:52
  • @Neil: By the way, is there a way to avoid the redundancy of specifying the type of the vector I'm feeding my function? To let it read it from the object itself? I mean without getting into dozens of lines of code? – Learning is a mess Oct 27 '14 at 17:21
  • 1
    `template pair mean_std(ITR the_beg, ITR the_end)` Use `typename ITR::value_type` to get the stored type. – Neil Kirk Oct 27 '14 at 17:32
  • @NeilKirk Thank you very much, bookmarking this ;) – Learning is a mess Oct 28 '14 at 11:34

0 Answers0