Solved

Posted on 2006-04-20
225 Views
Here is a function template that will accept two arguments. I must then write the function to accept three arguments and calculate the average of these arguments.  I have just included my template and function- no errors anywhere else.

template <class T>
T average(T a1, T b2);
T average(T a1,T b2, T c3);  //Overloaded function

template<class T>
T average(T a1, T b2);
T average(T a1, T b2, T c3);
{ if (number = 2)
average = (a1 + b2) / 2;

else if (number = 3)
average = (a1 +b2 +c3 ) / 3;
else
cout << "Try agian" <<endl;

return double average;
}

Here are the errors I am getting that is related to this code.

error C2146: syntax error : missing ';' before identifier 'average'
error C2501: 'T' : missing storage-class or type specifiers
error C2146: syntax error : missing ')' before identifier 'a1'
error C2501: 'average' : missing storage-class or type specifiers
error C2365: 'average' : redefinition; previous definition was a 'function'
error C2059: syntax error : ')'

I don't understand what is wrong with my template, it is basically written straight from the book.  Please help.

0
Question by:joew08

LVL 20

Expert Comment

hi  joew08,

seems you tried to declare 2 functions, try this, only the second:

template <class T>
T average(T a1,T b2, T c3);  //Overloaded function

template<class T>
T average(T a1, T b2, T c3)  // << remove the semicolon here
{ if (number = 2)
average = (a1 + b2) / 2;

else if (number = 3)
average = (a1 +b2 +c3 ) / 3;
else
cout << "Try agian" <<endl;

return double average;
}

hope it helps :)
ike
0

LVL 20

Expert Comment

this code should work:

template <class T> T average(T a1, T b2);
template <class T> T average(T a1,T b2, T c3);

template<class T>
T average(T a1, T b2)
{
return (a1 + b2) / 2
}

template<class T>
T average(T a1, T b2, T c3);
{
return (a1 +b2 +c3 ) / 3;
}

ike
0

LVL 20

Expert Comment

you said "basically written straight from the book". can you post the original, then we can see where you did go wrong.
in your code is a variable "number" .. where was it declared?

ike
0

Author Comment

said "basically written straight from the book". can you post the original, then we can see where you did go wrong.
in your code is a variable "number" .. where was it declared?

ike
-------------------------------------------------------------
The book example is as follows:

template<class T>
T larger(T x, T y);

template<class T>
T larger(T x, T y)
{if ........
}
-------------------------------------------------------------
So when I declare my functions and then overload one

template <class T> T average(T a1, T b2);
template <class T> T average(T a1,T b2, T c3); //overloaded function

I have to define both of them indepently?  Is this correct.

Joe

0

LVL 20

Accepted Solution

>> I have to define both of them indepently?  Is this correct.

yes, at least you have to define both of them. you only have to declare them, if the function is used in the code, before the definition occured.

// declaration
template <class T> T average(T a1, T b2);
template <class T> T average(T a1,T b2, T c3);

/* anywhere in the code the function is used, now at least  "template <class T> T average(T a1, T b2)" has to be declared above */
int a = average( b, c );

// definition
template <class T> T average(T a1, T b2)
{
// code here
}
template <class T> T average(T a1,T b2, T c3)
{
// code here
}

hope it helps :)
ike

0

Featured Post

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.