在const调用时声明字段“无法引用函数operator =(const memAddress&)–它是一个已删除的函数”

时间:2018-10-10 10:16:07

标签: c++ oop

我有一个存储和操作内存地址的结构。我决定将其中一个字段从int更改为const int,以确保它是只读的,突然间,我定义的operator-()发出以下消息:

  

函数“ memAddress :: operator =(const memAddress&)”(已声明   隐式)不能被引用-它是一个已删除的函数

这是代码,

struct memAddress
{
    // memory location variables
    int die = 0;
    int plane = 0;
    int page = 0;
    int column = 0;
    int block = _BLOCK_STARTING_ADDRESS;

    memAddress() {}

    memAddress(const memAddress &m)
    {
        die = m.die;
        plane = m.plane;
        page = m.page;
        column = m.column;
        block = m.block;
    }

    bool operator<(const memAddress &m)
    {
        if (die > m.die)
            return false;
        else if (die == m.die)
        {
            if (plane > m.plane)
                return false;
            else if (plane == m.plane)
            {
                if (block > m.block)
                    return false;
                else if (block == m.block)
                {
                    if (page > m.page)
                        return false;
                    else if (page == m.page)
                    {
                        if (column >= m.column)
                            return false;
                        else
                            return true;
                    }
                    else
                        return true;
                }
                else
                    return true;
            }
            else
                return true;
        }
        else
            return true;

    }

    bool operator==(const memAddress &m)
    {
        if (die == m.die &&
            plane == m.plane &&
            block == m.block &&
            page == m.page &&
            column == m.column)
        {
            return true;
        }

        return false;
    }

    bool operator<=(const memAddress &m)
    {
        if ((*this < m) || (*this == m))
            return true;

        return false;
    }

    bool operator>(const memAddress &m)
    {
        if (!(*this <= m))
            return true;

        return false;
    }

    bool operator>=(const memAddress &m)
    {
        if ((*this > m) || (*this == m))
            return true;

        return false;
    }

    memAddress operator-(const memAddress &m)
    {
        memAddress diff, a1, a2;

        if (*this < m)
        {
            a2 = m; // **error** 
            a1 = *this; // **error**
        }
        else
        {
            a2 = *this; // **error**
            a1 = m; // **error**
        }

        diff.die = a2.die - a1.die;
        diff.plane = a2.plane - a1.plane;
        diff.block = a2.block - a1.block;
        diff.page = a2.page - a1.page;
        diff.column = a2.column - a1.column;

        return diff;

    }

private:
    const int _BLOCK_STARTING_ADDRESS = 2; // **modifier added here**

};

我不知道为什么会这样-如果删除修饰符,代码可以正常工作。

为什么将字段修饰符更改为const会导致这种现象?我该如何解决?

2 个答案:

答案 0 :(得分:7)

您使用所有大写字母(以及使用它来初始化block)表明您打算将_BLOCK_STARTING_ADDRESS用作所有实例的类常量。

所以首先要使其静态

static const int _BLOCK_STARTING_ADDRESS = 2;

为什么?因为否则它是每个实例的数据成员。意味着每个实例都有一小部分必须为const,并且默认情况下您不能将其分配给该const位。因此,编译器无法为您生成默认的赋值运算符。

另一方面,也没有。以_[A-Z]开头的名称保留给C ++实现使用。为避免出现鼻恶魔的可能性,建议您更改其命名方案。也许甚至与less shouting的人在一起?

答案 1 :(得分:2)

由于您具有 const 非类类型(const int _BLOCK_STARTING_ADDRESS)的非静态数据成员,因此将删除默认的赋值运算符。

使成员static会生成默认的赋值运算符。

CPP草案(N4713)声明有关赋值运算符:

  

15.8.2复制/移动分配运算符
  ...
  7. 如果X具有默认值,则将X类的默认复制/移动赋值运算符定义为已删除
  (7.1)—具有非平凡的对应赋值运算符且X是类联合类的变体成员,或
  (7.2)-const非类类型的非静态数据成员(或其数组),或者
  (7.3)—引用类型的非静态数据成员,或
  (7.4)—无法复制/移动的类类型M(或其数组)的直接非静态数据成员,因为用于查找M的对应赋值运算符的重载分辨率(16.3)导致默认赋值运算符已删除或无法访问的歧义或函数。