将值分配给地图矢量容器的元素时发生访问冲突

时间:2011-11-01 02:33:27

标签: c++ templates map

我定义了一个模板类,如下所示:

template <class T, size_t IR_CAL_LEVELS> 
class CCalibrationTable
{
public:
// Default Constructor/Destructor
    CCalibrationTable(void);
    ~CCalibrationTable(void) {}

// Implementation
    void Decode(const unsigned char* pszData);

protected:
    int  m_nActiveSensorID;                                        // Sensor selection 1-primary sensor, 2-backup sensor (11) I*1
    std::map<std::string, std::vector<T> > m_mapCalibrationTables; // Calibration tables such as VIS 64-level albedo cnversion table, and 
                                                                   // IR Level-temperature conversion tables.
private:
    static const size_t VIS_CAL_LEVELS;
    static const size_t NUM_OF_GROUPS;
};

template<class T, size_t IR_CAL_LEVELS> 
const size_t CCalibrationTable<T, IR_CAL_LEVELS>::VIS_CAL_LEVELS = 64;

template<class T, size_t IR_CAL_LEVELS> 
const size_t CCalibrationTable<T, IR_CAL_LEVELS>::NUM_OF_GROUPS = 25;

template<class T, size_t IR_CAL_LEVELS> 
CCalibrationTable<T, IR_CAL_LEVELS>::CCalibrationTable() 
{   
    // For VIS channels
    m_mapCalibrationTables.insert(std::make_pair("VIS1Cal", std::vector<T>(VIS_CAL_LEVELS, 0)));
    m_mapCalibrationTables.insert(std::make_pair("VIS2Cal", std::vector<T>(VIS_CAL_LEVELS, 0)));
    m_mapCalibrationTables.insert(std::make_pair("VIS3Cal", std::vector<T>(VIS_CAL_LEVELS, 0)));
    m_mapCalibrationTables.insert(std::make_pair("VIS4Cal", std::vector<T>(VIS_CAL_LEVELS, 0)));

    // For IR channels
    m_mapCalibrationTables.insert(std::make_pair("IR1Cal", std::vector<T>(IR_CAL_LEVELS, 0)));
    m_mapCalibrationTables.insert(std::make_pair("IR2Cal", std::vector<T>(IR_CAL_LEVELS, 0)));
    m_mapCalibrationTables.insert(std::make_pair("IR3Cal", std::vector<T>(IR_CAL_LEVELS, 0)));
    m_mapCalibrationTables.insert(std::make_pair("IR4Cal", std::vector<T>(IR_CAL_LEVELS, 0)));
}    

template<class T, size_t IR_CAL_LEVELS> 
void CCalibrationTable<T, IR_CAL_LEVELS>::Decode(const unsigned char* pszData)
{   
    // Sensor selection 1-primary sensor, 2-backup sensor (11) I*1 
    m_nActiveSensorID = CBCDDecoder::SINTn(1, &(pszData[CBCDDecoder::IndexI2D(11)]));

    for (size_t nVISLevel = 0; nVISLevel < VIS_CAL_LEVELS; nVISLevel++) 
    {   
        int nOffset = (nVISLevel << 2);

        m_mapCalibrationTables["VIS1Cal"][nVISLevel] = CBCDDecoder::REALnm(4, 6, &(pszData[CBCDDecoder::IndexI2D(257                             + nOffset)]));
        m_mapCalibrationTables["VIS2Cal"][nVISLevel] = CBCDDecoder::REALnm(4, 6, &(pszData[CBCDDecoder::IndexI2D(257 + (VIS_CAL_LEVELS << 2)     + nOffset)]));
        m_mapCalibrationTables["VIS3Cal"][nVISLevel] = CBCDDecoder::REALnm(4, 6, &(pszData[CBCDDecoder::IndexI2D(257 + (VIS_CAL_LEVELS << 2) * 2 + nOffset)]));
        m_mapCalibrationTables["VIS4Cal"][nVISLevel] = CBCDDecoder::REALnm(4, 6, &(pszData[CBCDDecoder::IndexI2D(257 + (VIS_CAL_LEVELS << 2) * 3 + nOffset)]));
    }

    for (int nIRLevel = 0; nIRLevel < IR_CAL_LEVELS; nIRLevel++)
    {   
        int nOffset = (nIRLevel << 2);

        m_mapCalibrationTables["IR1Cal"][nIRLevel] = CBCDDecoder::REALnm(4, 3, &(pszData[CBCDDecoder::IndexI2D(1281                            + nOffset)]));
        m_mapCalibrationTables["IR2Cal"][nIRLevel] = CBCDDecoder::REALnm(4, 3, &(pszData[CBCDDecoder::IndexI2D(1281 + (IR_CAL_LEVELS << 2)     + nOffset)]));
        m_mapCalibrationTables["IR3Cal"][nIRLevel] = CBCDDecoder::REALnm(4, 3, &(pszData[CBCDDecoder::IndexI2D(1281 + (IR_CAL_LEVELS << 2) * 2 + nOffset)]));
        m_mapCalibrationTables["IR4Cal"][nIRLevel] = CBCDDecoder::REALnm(4, 3, &(pszData[CBCDDecoder::IndexI2D(1281 + (IR_CAL_LEVELS << 2) * 3 + nOffset)]));
    }
}

当我在另一个类中使用这种模板化类时,首先声明如下:

CCalibrationTable<double, 1024> m_CalibrationTable;

将解码值分配给我的地图矢量容器m_mapCalibrationTables时发生错误(访问冲突读取位置0x00000004)。正如您在构造函数中看到的那样,所有映射键以及它们各自的向量值都已准备就绪(保留空间并初始化为0)。

我的作业陈述有什么错误,例如&#34; m_mapCalibrationTables [&#34; VIS1Cal&#34;] [nVISLevel] = XXXXX&#34;?您最好不要怀疑我的解码器的返回值,例如REALnm和IndexI2D,它们分别只返回double和integer值。我检查了这些返回值,它们是正确的。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

如果我发表评论

m_nActiveSensorID = CBCDDecoder::SINTn(1, &(pszData[CBCDDecoder::IndexI2D(11)]));

并将对CBCDDecoder :: REALnm的所有调用更改为常量“0”。使用MSVC 2010编译时,代码运行正常。 我猜你在CBCDDecoder函数或ctor和Decode之间运行的代码中的任何其他地方都有一些副作用。