-1

I have a function, prototyped as below:

double gradeCalc (double midterm, double final, const vector<double>& hwGrades);

in my code I pass the variable homework, declared as

vector<double> homework;

into the third argument. I get the below error:

undefined reference to `gradeCalc(double, double, std::vector<double, std::allocator<double> >)'

In the textbook I'm working from, the function prototype is the same, yet there is no mention of this error. It seems the type of homework is not what I thought it was. What have I done wrong? (and what is an allocator for that matter?)

edit full function definition below as requested:

double gradeCalc (double midterm, double final, const vector<double> & hwGrades) {
    if (hwGrades.size() == 0) {
    throw domain_error("homework grades list empty");
    }

    double median = vdMedian(hwGrades);
    return 0.2 * midterm + 0.4 * final + 0.4 * median;
}

edit #2 found. silly typographical error. would still be interested to know what an allocator is. happy to ask in a seperate question.

  • 4
    Did you actually define the function? Providing a [mcve] will help. – NathanOliver Jul 07 '17 at 13:18
  • where is the `gradeCalc` defined? – dlmeetei Jul 07 '17 at 13:19
  • gradeCalc is defined in the program, I was trying to keep it minimal as it seems to be some kind of type mismatch? rather than whats inside the function. in any case i'll add it now :) – Matt Creighton Jul 07 '17 at 13:28
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – UnholySheep Jul 07 '17 at 13:32
  • You can upload the whole code to sites like pastebin. Having a working example would help. – Anoroah Jul 07 '17 at 13:33
  • Shouldn't the prototype have double for return type? – Paul H. Jul 07 '17 at 13:35
  • This question does not provide enough information to answer it. – underscore_d Jul 07 '17 at 13:36
  • found! missing ampersand in prototype. not a strange type mismatch as it seemed. would still be interested to know why the type of homework is not the same as i declared it with. – Matt Creighton Jul 07 '17 at 13:36
  • That means your declaration (not "prototype") is _not_ as you showed us. Please copy/paste your [MCVE] instead of wasting our time – Lightness Races in Orbit Jul 07 '17 at 13:47
  • _"would still be interested to know what an allocator is. happy to ask in a seperate question."_ Yes, that should be a separate question, but no, you don't need to ask it again: [What are allocators and when is their use necessary?](https://stackoverflow.com/questions/17848186/what-are-allocators-and-when-is-their-use-necessary) Note that question was closed, so it might be that it's considered too general for SO. But see also e.g. https://stackoverflow.com/questions/4254811/memory-management-stdallocator and https://stackoverflow.com/questions/826569/compelling-examples-of-custom-c-allocators – underscore_d Jul 10 '17 at 09:34

1 Answers1

0

The vector template is defined as

template<class _Ty, class _Alloc = allocator<_Ty> >
class vector: public _Vector_alloc<_Vec_base_types<_Ty, _Alloc> >

Note the deafult value for the second template parameter.

So when you write this

std::vector<double> homework;

it's actually the same as

std::vector<double, std::allocator<double>> homework;
Henrik
  • 23,186
  • 6
  • 42
  • 92