【问题标题】:stl::map assert [closed]stl::map 断言 [关闭]
【发布时间】:2011-10-09 03:21:23
【问题描述】:

我在 VS 2008 上运行以下代码

 typedef map<int ,string> ListofName;
 Class abc
 {
    ListofName m_List;
    ListofName  GetList(){ &m_List;}
 }

 ThredProc(void* args)
 {
     abc* pabc = (abs*)args;

     for( ListofName:: iterator itrList = GetList()->begin(); 
                  itrList != GetList()->end();
                  itrList++)
     {
       // some operation
     }

}

当列表为空时,执行 itrList ++ 时代码会崩溃。

【问题讨论】:

  • 请显示更接近实际代码的代码。
  • 你能举一个简单的例子来实际编译和重现问题吗?你这里的代码全错了。
  • 当列表为空时,itrList++ 不应该执行。你的真实代码有何不同?
  • -1:您发布的代码无法编译。

标签: c++ stl


【解决方案1】:

以下方法定义

ListofName  GetList(){ &m_List;}

完全错了!替换为

ListofName&  GetList(){ return m_List;}

并将其用作对对象的引用而不是指向对象的指针,这意味着使用“。”而不是“->”

 for( ListofName:: iterator itrList = GetList().begin(); 
              itrList != GetList().end();
              itrList++)

我建议您阅读更多有关 C++ 基础知识的内容。

【讨论】:

  • 我试过你的建议它不起作用。
  • 如果您需要更多建议,请向我们展示更多代码。请注意,我的实际建议是更多地了解 c++...
【解决方案2】:
#include <map>
#include <string>

typedef std::map<int ,std::string> ListofName;

class abc{
public:
    ListofName m_List;
    ListofName&  GetList(){ return m_List;}
 };

void testfunction(void* args){
     abc* pabc = (abc*)args;

     for( ListofName::iterator itrList = pabc->GetList().begin(); 
                  itrList != pabc->GetList().end();
                  itrList++)
     {
       // some operation
     }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 2014-02-12
    • 2010-12-23
    相关资源
    最近更新 更多