-1

我的代码放在这里很复杂。我将尝试解释我是如何执行地图操作的。

连接.h

struct mAttributeTable_Compare
{
bool operator()(char* s1, char* s2) const
{
    return (strcmp(s1,s2) < 0);
}
};
typedef struct
{
  map<Char*, Attribute*, mAttributeTable_Compare> mAttributeTable_API;
  map<Char*, Attribute*, mAttributeTable_Compare>::iterator iter_API;      

} Connection_vars;
class Attribute
{
    protected:
           Attribute(char*);

    public:
        Void            AddValue(Char* value);
        Char*           GetName();
        Char*           GetValue(Int index = 0); 

    private:
        Char*   mName;
        vector<Char*>   mValues;      
        Int     mType;
        Int     mCount;
};
class Connection
{
   public:
    Connection();
   Protected:
    Void  ProcessAttribute(int,Attribute*);
   private:
    map<Char*, Attribute*, mAttributeTable_Compare> mAttributeTable;
    map<Char*, Attribute*, mAttributeTable_Compare>::iterator iter;      
    void *mConnVars;
};

连接.cpp

#include "connection.h"
Connection()
{
    mConnVars = (Connection_vars *)calloc(1, sizeof(Connection_vars));  
    if(!mConnVars)                                                      
    {                                                                   
      return;                                                           
    }                                                                   

}
Void Connection::ProcessAttribute(int attrName, Attribute* attribute )
{
 Attribute* attr_API;
 Connection_vars *my_ConnectionVars = (Connection_vars *)mConnVars;
 if ( attrName == 10 )
 {
  attr_API = new Attribute("TraceOutput");
  Char* fileName = (Char*)attribute->GetValue(0);
  attr_API->AddValue(filename);

  /* this map variable insert operation is working*/ 
  mAttributeTable.insert(pair<Char*, Attribute*>(attr_API->GetName(), attr_API);

  /* But the below operation is returning a SEGMENTATION FAULT*/
  (my_ConnectionVars ->mAttributeTable_API).insert(pair<Char*, Attribute*>(attr_API->GetName(), attr_API);


}
Attribute::Attribute(Char* name)
{ 
    mName = strdup(name);
    mType = 0; 
    mCount = 0;
}

Char* Attribute::GetName()
{
    return mName;
}

Char* Attribute::GetValue(Int index)                        
{
    if (index >= 0 && index < (Int)mValues.size())
        return mValues[index];
    else
        return NULL;
}
Void Attribute::AddValue(Char* value)
{
    Char* temp = (Char*)strdup(value);              

    mValues.push_back(temp);
    mCount++;
}

在上面的 ProcessAttribute() 中,我无法从标头插入结构 (Connection_vars) 中声明的地图容器(mAttributeTable_API) 并获得 SEGMENTATION FAULT,因为我可以插入声明为的地图容器(mAttributeTable)类(连接)内的私有变量。我无法找到出错的原因。请帮我解决这个问题。非常感谢任何帮助!

提前致谢!

4

2 回答 2

0

在上面的 ProcessAttribute() 中,我无法插入地图容器(mAttributeTable_API)

'Void' 没有命名类型。

装运它!

于 2013-06-25T06:13:54.040 回答
0

我会说以下行:

mConnVars = (Connection_vars *)calloc(1, sizeof(Connection_vars));

不会正确初始化 Connection_vars 结构内的 std::maps 。它只会分配内存并用零初始化它们。

也许它有助于将上面的行替换为:

mConnVars = new Connection_vars;
于 2013-06-25T06:17:49.603 回答