C不能从map调用lambda表达式

我已经创建了一个温度转换类,我决定创建一个具有如下结构的转换表:
temperature | conversion formula

出于这个原因,我创建了一个std :: map,其中键作为温度指数,然后是公式.在这里你可以看到实现:

enum class TempType {Celsius,Fahrenheit,Kelvin,Rankine,Delisle,Newton,Reaumur,Romer};

class Temperature {
private:

  double value;   //value of the temperature
  TempType kind;  //Celsius,Fahrenheit...

  //conversion tables
  std::map<TempType,std::function<double(double)>> fromCelsius = {
      { TempType::Fahrenheit,[](double x) { return x * (9/5) + 32; } },{ TempType::Kelvin,[](double x) { return x + 273.15; } },{ TempType::Rankine,[](double x) { return (x + 273.15) * (9/5); } },{ TempType::Delisle,[](double x) { return (100 - x) * (3/2); } },{ TempType::Reaumur,[](double x) { return x * (33/100); } },{ TempType::Newton,[](double x) { return x * (4/5); } },{ TempType::Romer,[](double x) { return x * (21/40) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromFahrenheit = {
      { TempType::Celsius,[](double x) { return (x - 32) * (5/9); } },[](double x) { return (x + 459.67) * (5/9); } },[](double x) { return x + 459.67; } },[](double x) { return (212 - x) * (5/6); } },[](double x) { return (x - 32) * (11/60); } },[](double x) { return (x - 32) * (4/9); } },[](double x) { return (x - 32) * (7/24) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromKelvin = {
      { TempType::Celsius,[](double x) { return x - 273.15; } },{ TempType::Fahrenheit,[](double x) { return x * (9/5) - 459.67; } },[](double x) { return x * (9/5); } },[](double x) { return (373.15 - x) * (3/2); } },[](double x) { return (x - 273.15) * (33/100); } },[](double x) { return (x - 273.15) * (4/5); } },[](double x) { return (x - 273.15) * (21/40) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromrankine = {
      { TempType::Celsius,[](double x) { return (x - 491.67) * (5/9); } },[](double x) { return x - 459.67; } },[](double x) { return x * (5/9); } },[](double x) { return (671.67 - x) * (5/6); } },[](double x) { return (x - 491.67) * (11/60); } },[](double x) { return (x - 491.67) * (4/9); } },[](double x) { return (x - 491.67) * (7/24) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromDelisle = {
      { TempType::Celsius,[](double x) { return 100 - x * (5/9); } },[](double x) { return 212 - x * (6/5); } },[](double x) { return 373.15 - x * (2/3); } },[](double x) { return 671.67 - x * (6/5); } },[](double x) { return 33 - x * (11/50); } },[](double x) { return 80 - x * (8/15); } },[](double x) { return 60 - x * (7/20); } }
  };

  std::map<TempType,std::function<double(double)>> fromNewton = {
      { TempType::Celsius,[](double x) { return x * (100/33); } },[](double x) { return x * (60/11) + 32; } },[](double x) { return x * (100/33) + 273.15; } },[](double x) { return x * (60/11) + 491.67; } },[](double x) { return (33 - x) * (50/11); } },[](double x) { return x * (80/33); } },[](double x) { return x * (35/22) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromreamur = {
      { TempType::Celsius,[](double x) { return x * (5/4); } },[](double x) { return x * (9/4) + 32; } },[](double x) { return x * (5/4) + 273.15; } },[](double x) { return x * (9/4) + 491.67; } },[](double x) { return (80 - x) * (15/8); } },[](double x) { return x * (33/80); } },[](double x) { return x * (21/32) + 7.5; } }
  };

  std::map<TempType,std::function<double(double)>> fromromer = {
      { TempType::Celsius,[](double x) { return (x - 7.5) * (40/21); } },[](double x) { return (x - 7.5) * (24/7) + 32; } },[](double x) { return (x - 7.5) * (40/21) + 273.15; } },[](double x) { return (x - 7.5) * (24/7) + 491.67; } },[](double x) { return (60 - x) * (20/7); } },[](double x) { return (x - 7.5) * (22/35); } },[](double x) { return (x - 7.5) * (32/21); } }
  };

public:
}    
#endif // TEMPERATURE_H

现在,如果我想从温度对象转换为另一个,我使用此代码

Temperature x(20,TempType::Celsius); //20 °C
double s = x.convertTo(TempType::Fahrenheit); //convert 20 °c to 68 °F

转换代码是这样的:

double Temperature::convertTo(const TempType& temperature) const {

  if (temperature == kind) {
    return value;
  }

  double result = -1;

  switch (temperature) {
    case TempType::Celsius:
      result = fromCelsius.at(kind)(value);
      break;
    case TempType::Fahrenheit:
      result = fromFahrenheit.at(kind)(value);
      break;
    case TempType::Kelvin:
      result = fromKelvin.at(kind)(value);
      break;
    case TempType::Rankine:
      result = fromrankine.at(kind)(value);
      break;
    case TempType::Delisle:
      result = fromDelisle.at(kind)(value);
      break;
    case TempType::Newton:
      result = fromNewton.at(kind)(value);
      break;
    case TempType::Reaumur:
      result = fromreamur.at(kind)(value);
      break;
    case TempType::Romer:
      result = fromromer.at(kind)(value);
      break;
    default:
      break;
    }

  return result;

}

我使用地图的键(TempType),然后我将参数传递给lambda.返回的结果总是0.任何想法?

我正在使用QTCreator和mingw.

解决方法

在C 4/5中,21 / 40,5 / 6等都是零,因为除法是以整数运算执行的.

你需要写4./5.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...