Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Overloading a function template

Posted on 2006-04-20
5
Medium Priority
?
237 Views
Last Modified: 2010-04-01
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
Comment
Question by:joew08
  • 4
5 Comments
 
LVL 20

Expert Comment

by:ikework
ID: 16499922
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

by:ikework
ID: 16499961
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

by:ikework
ID: 16499975
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

by:joew08
ID: 16501379
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

by:
ikework earned 1400 total points
ID: 16501488
>> 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question