#include <iostream> using namespace std; template <typename T> T max5(const T(&)[5]); int main() { double arr1[] { 2.5, 32.321, -442.4, 41.321, 34.005 }; int arr2[]{ 3, 45, 0, 412, -32}; cout << max5(arr1); cout << max5(arr2); cin.get(); return 0; } template <typename T> T max5(const T (&arr)[5]) { T thebiggest = 0; for (int i = 0; i < 5; i++) arr[i] > thebiggest ? thebiggest = arr[i] : continue; return thebiggest; } 

Here is what he writes: error C2760: syntax error: expected the token "<No data>, not" <No data> "note: A diagnostic message originated in the function created by the compiler" T max5 (const T (&) [5]) "function must accept an array of 5 elements of the base type and return the largest one.

  • Syntax is meaningless. Because the code is not compiled. What does continue inside the statement ?: ? - AnT

2 answers 2

Maybe you meant

 #include <iostream> using namespace std; template <typename T> T max5(const T(&arr)[5]) { T thebiggest = arr[0]; for(int i = 1; i < 5; ++i) if (arr[i] > thebiggest) thebiggest = arr[i]; return thebiggest; } int main() { double arr1[]{ 2.5, 32.321, -442.4, 41.321, 34.005 }; int arr2[]{ 3, 45, 0, 412, -32 }; cout << max5(arr1) << endl; cout << max5(arr2) << endl; cin.get(); return 0; } 

Well, for example, why do you continue ? What is i in the body of the template? The ternary operator compares something that you don’t understand with what, and then tries to perform completely different actions (and not expressions of the same type).

In general, I'm sorry, but you have written such that one question arises - why program, having smoked? :)

  • Sorry, I copied the wrong code, one second - Zondic
  • and yes, thanks, your code is working. I’ll launch the debugger and watch, thanks again. - Zondic
  • Sorry, but could you tell me what is wrong with my code (in the modified version)? - Zondic
  • 2
    The branches of the ternary operator should be, roughly speaking, of the same type. And you have one branch has some value, the second - no. Given priorities, by the way, do you compare arr[i] with the result of thebiggest ? thebiggest = arr[i] : continue ternary operator thebiggest ? thebiggest = arr[i] : continue thebiggest ? thebiggest = arr[i] : continue - so how is it more continue or less? :) Yes, and the logic is lame: if all values ​​in the array are less than 0, then you think that the greatest is zero? - Harry

Answered here : the second and third arguments of the "three-way operator" are expressions, not expressions, not statements.