【问题标题】:Structure initialization结构初始化
【发布时间】:2011-02-23 19:50:32
【问题描述】:

我使用了以下结构

template <typename Item>
struct TSet
{
    typedef std::set <int, comparator <Item>  >  Type;
};

作为结构的数据成员

template <typename Item>
struct TObject
{ 
   int code; 
   ...
   typename TSet <Item> ::Type indices;

   TObject ( const List <Item> *list ) : code( 0 ), indices ( list ) {}
 };

在哪里

 template <typename Item>
 struct TList
 {
    typedef std::vector <Item> Type;
 };

template <typename Item>
class List
{
    private:
            typename TList <Item>::Type items;
};

但是我已经把数据模型改成了

template <typename Item> 
class TSet : public std::set <int, comparator <Item>  >
{
};

template <typename Item>
struct TObject
{ 
  int code; 
  ...
  typename TSet <Item> indices;

  TObject ( const List <Item> *list ) : code ( 0 ), indices ( list ) {} //Error: Can not convert parameter 1 const List <Item> to const TSet <Item>
 };

并且结构初始化存在问题。

Error: Can not convert parameter 1 const List <Item> to const TSet <Item>

问题出在哪里?

【问题讨论】:

  • 什么是 List 类?您将如何从 const List* 构造 TSet
  • 我看不到List 是什么。另外,不要公开继承标准容器。有人,有时会尝试多态地使用它并破坏你的代码。

标签: c++ struct initialization


【解决方案1】:

如果您没有编写转换,为什么会期望从List 转换为TSet?另外,indices 的类型需要在前面加上typename,因为它是依赖类型。

【讨论】:

  • 你能给我一个简短的例子吗?
  • 我将声明重写为 typename TSet 索引。项目列表仅用于比较器比较器 的初始化。我不确定我是否需要转换函数...
  • @Robo:看看新的代码,你希望List&lt;Item&gt;在你的初始化中如何转换为TSet&lt;Item&gt;?没有转换运算符或构造函数。
  • 但是旧模型也没有任何转换功能:索引( list )仅初始化比较器(并且有效)...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多