【问题标题】:std::vector inserting std::pairstd::vector 插入 std::pair
【发布时间】:2013-08-18 01:09:54
【问题描述】:

我在尝试使用以下代码在 std::vector 中插入 std::pair 时遇到问题:

template <class R>
class AVectorContainner
{
public:
  AVectorContainner()
       {
         mVector= new std::vector<entry>;
       }
  typedef std::pair<int ,R *> entry;

  void insert(R* aPointer, int aID)
  {
     entry aEntry;
     aEntry=std::make_pair(aID,aPointer );
     mVector->push_back(aEntry);

  }
private: 
  std::vector<entry> * mVector;
}

这是主文件的一部分,我声明了一个类的指针,然后在模板类的初始化中使用它。

在main.cpp中:

    int main()
    {
        SomeType * aTipe= new SomeType;
        int aID=1;
        AVectorContainer<SomeType> * aContainer= new AVectorContainer;
        aContainer->insert(aTipe,aId);//error line
delete aTipe;
delete aContainer;
        return 0;
    }

编译器输出:

error: non-static reference member 'const int& std::pair<const int&, SomeType *>::first', can't use default assignment operator

error: value-initialization of reference type 'const int&'

【问题讨论】:

  • 这段代码中有太多new
  • ... 而不是要看到的复制 ctor 或赋值运算符。呃。
  • 那么,Ricardo,您是来自 Java 还是 C#?
  • @Yakk 你不应该修复 OP 的拼写错误,除非 OP 自己修复它们。
  • @Ricardo_arg -1 显示真实代码,否则会猜测太多。

标签: c++ templates stl stdvector std-pair


【解决方案1】:

原始发布者未能实际发布导致问题的代码。

他已经用正确的代码编辑了我的帖子,证明了这个问题。演示问题的代码如下:

template <class R, typename B=int>
class AVectorContainer
{
public:
  AVectorContainer() {}
  typedef R* ptr_Type;
  typedef const B & const_ref_Type;
  typedef std::pair<const_ref_Type ,ptr_Type> entry;


  void insert(ptr_Type aPointer, const_ref_Type aID) {
     entry aEntry=std::make_pair(aID,aPointer);
     mVector.push_back(aEntry);
  }
private:
  std::vector<entry> mVector;
};

class SomeType
{
public:
  SomeType(){ x=5; }
  ~SomeType(){ }
  int x;
};

int main()
{
  SomeType * aTipe= new SomeType;
  int aID=1;
  AVectorContainer<SomeType> aContainer;
  aContainer.insert(aTipe,aID);
  return 0;
}

编译器输出:

/usr/include/c++/4.7/bits/stl_pair.h:88: error: non-static reference member 'const int& std::pair<const int&, SomeType*>::first', can't use default assignment operator

缺陷在于以下几行:

  typedef R* ptr_Type;
  typedef const B & const_ref_Type;
  typedef std::pair<const_ref_Type ,ptr_Type> entry;
  std::vector<entry> mVector;

在这里,原始发布者尝试制作包含常量引用的pairs 的vector,然后这样做:

entry aEntry;
aEntry=std::make_pair(aID,aPointer )

这会尝试将一对分配给另一对。但是const&amp; 变量不能分配给另一个变量——它们可以从另一个const&amp; 构造(初始化),但不能分配。

一个简单的解决方法是:

entry aEntry=std::make_pair(aID,aPointer )

这样我们就不会从另一个entry 构造aEntry,而不是默认构造aEntry(这也是非法的:必须初始化const&amp;),然后分配给它。

【讨论】:

  • 没错,我并没有太注意实际的代码。我太忙了SnD'ing错别字!
  • 说真的??这是一个解决方案??只是检查类型并添加一些只是为显示提议而编写的类?。
  • @Ricardo_arg 这不是什么大问题。您的代码充满了拼写错误——TheOtherGuy 修复了这个问题。我注意到你还有一堆不必要的news:我没有编辑 TheOtherGuy 的答案来删除它们,而是把它扔在这里。如果 ThatOtherGuy 改进了他的答案,我会删除我的。
  • 这里有一个类似的帖子:stackoverflow.com/questions/634662/…,这个也面临同样的问题。对该问题的评论之一是:blog.copton.net/archives/2007/10/13/stdvector/index.html。代码无法编译,所以用分号和其他问题填充的 fixit 似乎并没有提高问题的质量,编译器再次响起相同的输出,抱歉,那些 anwser 没有回答问题。
  • 我写的代码可以编译。我什至链接到一个活生生的例子。如果你没有,也许你的编译器坏了,或者你的代码和我的不同。 @ricardo_arg
【解决方案2】:

修正了所有错别字,比较两者……他确实喜欢 20 行中的 100!

#include <vector>
#include <utility>

template <class R>
class AVectorContainer
{
public:
  AVectorContainer()
       {
         mVector= new std::vector<entry>;
       }
  typedef std::pair<int ,R *> entry;

  void insert(R* aPointer, int aID)
  {
     entry aEntry;
     aEntry=std::make_pair(aID,aPointer );
     mVector->push_back(aEntry);

  }
private: 
  std::vector<entry> * mVector;
};

class SomeType
{
public:
    SomeType(){ x=5; }
    ~SomeType(){ }
    int x;
};

int main()
{
    SomeType * aTipe= new SomeType;
    int aID=1;
    AVectorContainer<SomeType> * aContainer= new AVectorContainer<SomeType>;
    aContainer->insert(aTipe,aID);//error line
    return 0;
}

【讨论】:

    【解决方案3】:

    在修正所有错别字(ContainneraId、缺少; 等)后,代码编译得很好。

    【讨论】:

      猜你喜欢
      • 2011-01-01
      • 1970-01-01
      • 2011-10-29
      • 2013-03-08
      • 2018-05-31
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多