我想尝试从每个值返回一个值,但由于错误使用条件运算符而导致错误。
#include<iostream>
using namespace std;
enum class Fruit { apple, orange, pear };
enum class Color { red, green, orange };
// no problem till here but conditional operators are causing problems,i am
// getting so many errors.does anyone knows the right way of writing them here?
template <typename T> struct Traits;
template <>
struct Traits<Fruit>
{
static string name(int index)
{
int var=(index<=2?(index!=0?(index==1? return "orange": return "pear"):return "apple"): return "unknown");
}
};
template <>
struct Traits<Color>
{
static string name(int index)
{
int var=(index<=2?(index!=0?(index==1? return "green": return "orange"):
return "red"): return "unknown"); //something wrong with the use of the operators
}
};
驱动程序代码
int main()
{
int t = 0; std::cin >> t;
for (int i=0; i!=t; ++i) {
int index1; std::cin >> index1;
int index2; std::cin >> index2;
cout << Traits<Color>::name(index1) << " ";
cout << Traits<Fruit>::name(index2) << "\n";
}
}
如何正确使用它们