1

If I remove my function prototype and move the function from the bottom to the top everything works fine and the function can accept either a float or and int as a datatype. Aren't you normally supposed to prototype functions? Also, I am a bit curious as to why the function only works if it is at the top. I am pretty sure it is a scoping issue, but it is going over my head for some reason.

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

template <class tyler>              // function template
tyler Addition(tyler, tyler);       // function prototype


int main()
{
setprecision(2); //limits decimal output to 2 places
int num1, num2;
float num3, num4;

cout << "Enter your first number: \n";
cin  >> num1;
cout << "Enter your second number: \n";
cin  >> num2;
cout << num1 << " + " << num2 << " = " << Addition(num1, num2);

cout << "Enter your third number: (round 2 decimal places, e.x. 7.65) \n";
cin >> num3;
cout << "Enter your fourth number: (round 2 decimal places, e.x. 7.65 \n";
cin >> num4;
cout << num3 << " + " << num4 << " = " << Addition(num3, num4);

cin.clear();                // Clears the buffer 
cin.ignore(numeric_limits<streamsize>::max(), '\n');  // Ignores anything left in buffer
cin.get();                  // Asks for an input to stop the CLI from closing.
return 0;
}

tyler Addition(tyler num1, tyler num2)
{
    return (num1 + num2);
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065

1 Answers1

1

Here's the implementation of the function as it's written:

tyler Addition(tyler num1, tyler num2)
{
    return (num1 + num2);
}

Notice that this is not a template function, and so C++ treats it as though it actually takes in arguments of type tyler rather than treating tyler as a placeholder.

If you want to define the template function later on, that's fine! Just repeat the template header:

/* Prototype! */
template <typename tyler>
tyler Addition(tyler num1, tyler num2)


/* ... other code! ... */


/* Implementation! */
template <typename tyler>
tyler Addition(tyler num1, tyler num2)
{
    return (num1 + num2);
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • I was following thenewboston's template function tutorial and it was the same only he didn't prototype it. I normally prototype my functions, so I was trying to find a way to do that. Other than that my code is exactly the same as his. Besides that, can I assume the second box of code you gave me would be a proper example of a template function? – Tyler Averette Oct 09 '17 at 01:28
  • I'll be honest, no video tutorial of C++ that I've seen correctly teaches the basics and the advanced portions, thenewboston definitely included. I suggest picking up a book on C++ instead. – user975989 Oct 09 '17 at 01:31
  • I just updated my answer to more clearly indicate that you can keep your prototype *and* define the template function later on. You just need to include the template header in both cases. – templatetypedef Oct 09 '17 at 02:01