-5

When I wrote a C++ code and compiled it by the clang++ compiler,

error: expected expression
template <typename T>
^

was represented.

Why did this error appeared and how do I fix it?

#include<iostream>
using namespace std;

int main() {

template <typename T>
T sum(T a, T b) {
return a+b;
}

cout <<"Sum = " << sum( 2.1, 7.9 ) << endl;

return 1;
}

1 Answers1

8

You cannot define a function within main. Move the definition outside

#include <iostream>

template <typename T>
T sum(T a, T b)
{
    return a + b;
}

int main()
{
    std::cout << "Sum = " << sum(2.1, 7.9) << std::endl;  
    return 0;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218