Overloading a function template

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.  

joew08Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ikeworkCommented:
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
ikeworkCommented:
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
ikeworkCommented:
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
joew08Author Commented:
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
ikeworkCommented:
>> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.