目标

我正在实现IntegerRing,这是抽象代数中的结构。这种类型的戒指是加成下的一个Abelian小组(我已经实现了)。振铃配备了两个运算符+和*。

选择实现

因此,我决定将IntegerGroup定义为具有GroupElement且具有运算符的类。完整的工作代码如下:

整数组

#ifndef DATAGROUP_H
#define DATAGROUP_H

#include "Array.h"

#include <iostream>

// This group is the integers mod n
// multiplication in integer group is simply integer addition modulo n
class IntegerGroup
{
    public:
        IntegerGroup();
        IntegerGroup(int);
        class GroupElement
        {
            int m;
            IntegerGroup* group;
            public:
                GroupElement();
                GroupElement(int, IntegerGroup*);
                ~GroupElement();
                GroupElement operator*(const GroupElement&);
                GroupElement operator*=(const GroupElement&);
                bool operator==(const GroupElement&);
                bool operator!=(const GroupElement&);
                int val() const;
                friend std::ostream& operator<<(std::ostream& o, const GroupElement& e)
                {
                    return (o << e.m);
                }

        };
        GroupElement identity() const;
        int size() const;
        friend std::ostream& operator<<(std::ostream& o, const IntegerGroup& g)
        {
            return (o << g.elements);
        }
    private:
        int n;
        //GroupElement * identity;
        Array<GroupElement> elements;
        void createNewElement(int);
};

#endif

IntegerGroup.cpp
#include "IntegerGroup.h"

#include <new>
#include <iostream>

IntegerGroup::IntegerGroup()
{

}

IntegerGroup::IntegerGroup(int n)
 : n(n), elements(Array<IntegerGroup::GroupElement>(n))
{
    //this is to have integers in [0,n-1]
    for (int j = 0; j < n; j++)
    {
        this->createNewElement(j);
    }
}

void IntegerGroup::createNewElement(int m)
{
    // create new GroupElement
    GroupElement newElement(m, this);
    // store it at index m in elements
    this->elements[m] = newElement;
}

IntegerGroup::GroupElement::GroupElement()
    : group(0)
{

}


IntegerGroup::GroupElement::GroupElement(int m, IntegerGroup * g)
    : group(g)
{
    // this->m must be in [0, g->size() - 1]
    this->m = m % g->size();
    if (this->m < 0) this->m = g->size() + this->m;
}

IntegerGroup::GroupElement::~GroupElement()
{
    if (this->group)
    {
        this->group = 0;
    }
}

IntegerGroup::GroupElement IntegerGroup::identity() const
{
    // IntegerGroup consists of all integers in [0, n-1], and identity is 0
    return this->elements[0];
}

// this group is simply the integers mod n, and should be populated integers in [0,n-1]
// thus, multiplication is simply a matter of returning the element at index (a+b)%n
IntegerGroup::GroupElement IntegerGroup::GroupElement::operator*(const IntegerGroup::GroupElement& b)
{
    // if the group is not defined
    if (!this->group)
        // we simply perform integer multiplication
        return GroupElement(this->val() * b.val());
    // otherwise, perform group multiplication
    return GroupElement((this->val() + b.val()) % this->group->size());
}

IntegerGroup::GroupElement IntegerGroup::GroupElement::operator*=(const IntegerGroup::GroupElement& b)
{
    return ((*this) = (*this) * b);
}

bool IntegerGroup::GroupElement::operator==(const IntegerGroup::GroupElement& b)
{
    return this->m == b.m;
}

bool IntegerGroup::GroupElement::operator!=(const IntegerGroup::GroupElement& b)
{
    return !(*this == b);
}

int IntegerGroup::GroupElement::val() const { return this->m; }

int IntegerGroup::size() const { return this->n; }

Array.cpp,Array.h只是模板包装器类。该代码也已经起作用。您可以在此处找到on的文件GitHub,也可以使用std::vector代替。 (现在我想到现在可以做到这一点。)

问题

当我尝试创建IntegerRing并进行编译时,我遇到了许多奇怪的错误,其中大多数与使用私有(private)类数据的类自己的函数有关。

到目前为止,这是我的IntegerRing的实现:

整数环
#ifndef INTEGERRING_H
#define INTEGERRING_H

#include "IntegerGroup.h"
#include "Operators.h"

class IntegerRing : public IntegerGroup
{
    public:
        class Element : public IntegerGroup::GroupElement
        {
            public:
                using IntegerGroup::GroupElement;
                /*Element();
                Element(int);
                Element(int, IntegerRing*);
                ~Element();*/
                operator IntegerGroup::GroupElement() { return IntegerGroup::GroupElement(); }
                Element(const IntegerGroup::GroupElement& el)
                {
                    // copy everything from el into *this
                    this->m = el.m;
                    this->group = el.group;
                }
                /*Element operator+(const Element&);
                Element operator-(const Element&);
                Element operator*(const Element&);
                Element operator+=(const Element&);
                Element operator-=(const Element&);
                Element operator*=(const Element&);*/

        };
        Element identity(Operators);
    private:

};

#endif

IntegerRing.cpp
#include "IntegerRing.h"
#include "IntegerGroup.h"
#include "Operators.h"

/*IntegerRing::Element::Element()
{

}*/

/*IntegerRing::Element(const IntegerGroup::GroupElement& el)
{
    // copy everything from el into *this
    this->m = el.m;
    this->group = el.group;
}
/*
IntegerRing::Element IntegerRing::Element::operator+(const IntegerRing::Element& b)
{
    // IntegerRing is simply Abelian group under addition
    // thus, we treat the elements like group elements first, multiply under that group, and cast to ring elements
    return (IntegerRing::Element)(((IntegerGroup::GroupElement)(*this)) * ((IntegerGroup::GroupElement)b));
}

IntegerRing::Element IntegerRing::Element::operator-(const IntegerRing::Element& b)
{
    int val;
    // if this has a group
    if (this->group)
    {
        // compute (this->m - b.m) % this->group->size()
        val = (this->m - b.m) % this->group->size();
        // if that value is negative, add this->group->size() to it
        if (val < 0) val = this->group->size() + val;
    }
    // otherwise, val is simply the integer difference of this->m,b.m
    else val = this->m - b.m;
    // return element with this value
    return Element(val);
}

IntegerRing::Element IntegerRing::Element::operator*(const IntegerRing::Element& b)
{
    if (this->group)
        return IntegerRing::Element((this->m - b.m) % this->group->size());
    return IntegerRing::Element(this->m - b.m);
}

IntegerRing::Element IntegerRing::Element::operator+=(const IntegerRing::Element& b)
{
    return ((*this) = (*this) + b);
}

IntegerRing::Element IntegerRing::Element::operator-=(const IntegerRing::Element& b)
{
    return ((*this) = (*this) - b);
}

IntegerRing::Element IntegerRing::Element::operator*=(const IntegerRing::Element& b)
{
    return ((*this) = (*this) * b);
}
*/
IntegerRing::Element IntegerRing::identity(Operators op)
{
    // if op is ADDITIVE
    if (op == ADDITIVE)
        // return what the base version of this method would return
        return (IntegerRing::Element)(((IntegerGroup::GroupElement*)this)->identity());
    // multiplicative identity requested, and it is 1
    return (IntegerRing::Element)this->elements[0];
}

运算符
#ifndef OPERATORS_H
#define OPERATORS_H

enum Operators
{
    ADDITIVE, MULTIPLICATIVE
};

#endif

编译器认为IntegerRing::Element的副本构造函数实际上是一个返回int的函数。

错误的屏幕截图

这是错误的屏幕截图:c&#43;&#43; - 内部派生成员-LMLPHP

如何解决所有这些问题?

最佳答案

原因是您无法访问类(class)的私有(private)字段。
继承/嵌套类不会对此进行更改。(异常是内部类始终可以访问其所在类的任何成员(自C++ 11起))

对于日志中的第一个错误using IntegerGroup::GroupElement;应该是usingIntegerGroup::GroupElement::GroupElement;内的IntegerRing::Element,顺便说一句,我看不到需要此类。

关于c++ - 内部派生成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39358793/

10-09 13:34