【问题标题】:Evaluating the subscript operator in the watch window在监视窗口中评估下标运算符
【发布时间】:2013-07-04 12:00:31
【问题描述】:

我有一个简单的数组包装类,如下所示:

class MyArray
{
    int * m_Data;
    int m_Size;

public:
    MyArray(int aSize) : m_Size(aSize), m_Data(new int[aSize])
    {
    }

    int & operator [](int aIndex)
    {
        return m_Data[aIndex];
    }

    const int & operator [](int aIndex) const
    {
        return m_Data[aIndex];
    }
};

MyArray a(10);

每当我尝试在调试器中评估下标运算符时(快速观察、即时窗口等):例如a[0],我收到 a[0] no operator "[]" matches these operands 错误。我知道我可以挖掘类字段以获取数组的内容。但是复制代码行的一部分并在监视窗口中对其进行评估要容易得多。 我尝试删除 const 和非常量 [] 运算符。我也尝试使用 () 运算符,它也不起作用,但它给出了不同的错误消息。我在 VS2012 和 VS2013 Preview 中试过这个:同样的事情。 有没有什么办法解决这一问题?

如果我用成员函数替换下标运算符:

int & Item(int aIndex)
{
    return m_Data[aIndex];
}

然后监视窗口能够向我显示结果。但我更喜欢使用下标运算符。

【问题讨论】:

    标签: visual-c++ visual-studio-debugging


    【解决方案1】:

    我找到了一个解决方案,虽然不是很方便,但似乎可行。如果我使用运算符调用的扩展形式,那么它可以在 VC++2012 中使用:

    a.operator[](0)
    

    我不清楚为什么这两种形式与 VC++ 调试器不同。于是我发了一个新问题here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多