-5 c++ templates expected-exception clang++
当我编写C++代码并通过clang ++编译器编译它时,
error: expected expression
template <typename T>
^
Run Code Online (Sandbox Code Playgroud)
有代表.
为什么会出现此错误,如何解决?
#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;
}
Run Code Online (Sandbox Code Playgroud)
你无法在其中定义一个功能main.将定义移到外面
#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;
}
Run Code Online (Sandbox Code Playgroud)