【问题标题】:C++ map template argument is invalidC++ 地图模板参数无效
【发布时间】:2018-03-09 23:54:40
【问题描述】:

我正在尝试创建一个映射,其值是我在另一个文件中定义的类;即FoodItemLookup。但是,C++ 给了我错误:

模板参数 1 无效

我觉得这很奇怪,因为我没有使用任何模板。

如果有人能向我解释我做错了什么,我将不胜感激。我不认为我可以发布所有代码,但这里是有问题的 sn-ps:

制作地图的尝试:

std::map<string, FoodItemLookup> foodCatalogue;

FoodItemLookup.h 文件的简要介绍:

#ifndef FOODITEMLOOKUP
#define FOODITEMLOOKUP

#include <string>

class FoodItemLookup
{
  private:
  ...

  public:

  //constructor
  FoodItemLookup(std::string upc, int shelfLife, std::string name);

  //copy constructor
  FoodItemLookup(const FoodItemLookup &other);

  //destructor
  ~FoodItemLookup();

  ...
);

#endif

【问题讨论】:

  • 确切错误信息是什么?
  • 您没有发布足够的代码供任何人帮助您。我们不得不猜测的事情太多,但我们无法得出结论。
  • 如果你在声明地图之前没有做任何特别的事情,你必须在那里使用std::string,就像在类声明中一样。

标签: c++ templates dictionary stdmap invalid-argument


【解决方案1】:

您的第一个模板参数中应该有 std:: in from string

std::map<std::string, FoodItemLookup> foodCatalogue;

“我觉得这很奇怪,因为我没有使用任何模板。”

其实std::map 使用模板。这就是为什么在实例化 std::map 时需要在 &lt;&gt; 之间传递 2 种类型(此处为 std::stringFoodItemLookup)。

More information on templates.

【讨论】:

  • 哦...哎呀。我以为我的课出了点问题。总是小事。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 2012-06-10
  • 2013-03-09
相关资源
最近更新 更多