【问题标题】:Type problem when including tuple包含元组时的类型问题
【发布时间】:2010-04-22 03:07:40
【问题描述】:

我正在使用带有 Feature Pack 1 的 Visual Studio 2008。

我有一个像这样的 typedef typedef std::tr1::tuple<std::string, std::string, int> tileInfo 具有类似const tileInfo& GetTile( int x, int y ) const 的功能。

在实现文件中,函数具有完全相同的签名(添加了类名限定符),我收到了redefinition: different type modifiers 错误。它似乎在寻找 int& 而不是 tileInfo&

当我将鼠标悬停在标题中的函数类型上时,即tileInfo& 它会弹出一个小条,上面写着static const int tileInfo。我认为这可能是问题所在,但我不确定该怎么做。这让我相信编译器认为std::tr1::tuple<std::string, std::string, int>static const int

感谢您的帮助。

附:这是一个模拟相同情况的示例,只是压缩到最低限度。

#include <tuple>

class Blah {
 public:
  typedef std::tr1::tuple<std::string, std::string, int> tileInfo;
  tileInfo& GetTile( int x, int y ); // When you mouse over tileInfo in this line, it says static const int
  ...
};

【问题讨论】:

    标签: c++ tuples tr1


    【解决方案1】:

    确保您包含带有typedef 的头文件。

    听起来编译器看不到typedef,所以tileInfo的类型默认为int

    【讨论】:

    • 包括在内。我最后描述的实际上显示在标题中。函数的签名在公共可见性的 typedef 下方。
    • 在定义typedef之前是否包含tuplestring
    • 我认为这可能与我没有正确安装 Feature Pack 1 或其他原因有关。
    • typedef 标头是否包含在声明 GetTile 的 标头 中?或者只是在源文件中?如果它认为它在标题中是 int 导致我相信它无法弄清楚它是什么类型。
    • 是的,它无法弄清楚类型是什么,我们试图想象他的代码来弄清楚为什么......如果我们看到一些示例代码可能会更容易它是如何组织的...
    【解决方案2】:

    似乎当使用 typedef 作为返回类型或局部变量时,即使在类中,我也必须使用类名来限定它。例如标题中的GetTile签名应该是TileMap::tileInfo&amp; GetTile( int x, int y );我认为当函数在具有typedef的类中时你不需要这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 2014-11-15
      相关资源
      最近更新 更多