silent
silent

Reputation: 2904

template function error

I have function which takes in an parameter of a class called "Triple", and am returning the averge of 3 values of type float.

template <typename ElemT>
float average(Triple ElemT<float> &arg){
    float pos1 = arg.getElem(1);
    float pos2 = arg.getElem(2);
    float pos3 = arg.getElem(3);

    return ( (pos1+pos2+po3) /3 );
}

when i try compiling this i get

q2b.cpp:32: error: template declaration of `float average'
q2b.cpp:32: error: missing template arguments before "ElemT"

not quite sure what this means.

Upvotes: 0

Views: 1418

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490048

Right now, it's not clear what you intend the template parameter to mean. It appears that a non-template function should work fine:

float average(Triple const &arg) {
    return (arg.getElem(1) + arg.getElem(2) + arg.getElem(3)) / 3.0f;
}

If Triple is itself a template that can be instantiated over different possible types, you could do something like this:

template <class T>
T average(Triple<T> const &arg) { 
    return (arg.getElem(1) + arg.getElem(2) + arg.getElem(3)) / T(3.0);
}

Upvotes: 0

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

Triple ElemT<float> &arg is not a valid type

Do you mean Triple<ElemT> &arg?

Upvotes: 0

sth
sth

Reputation: 229563

The declaration of the function parameter uses wrong syntax. Maybe you meant to write this:

template <typename ElemT>
float average(Triple<ElemT> &arg){
  ...
}

Or, if the function should just be specific to Triples of floats:

float average(Triple<float> &arg){
  ...
}

Upvotes: 5

Related Questions