Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过组合实现数学向量类_C++_Boost_Iterator_Operator Overloading_Stdvector - Fatal编程技术网

C++ 通过组合实现数学向量类

C++ 通过组合实现数学向量类,c++,boost,iterator,operator-overloading,stdvector,C++,Boost,Iterator,Operator Overloading,Stdvector,我开始编写一个类,它的行为很像std::vector,但带有一些数学运算。主要是向量的范数和重载重要的数学运算符(+、-等),它们将按元素进行加、减运算 这个类发布在下面,我使用了boost::operators来编写所有的数学运算符,它们都能完美地工作。我在实现迭代器时遇到了一些问题。我尝试将迭代器作为嵌套类编写,并使用boost::iterator获得std::vector迭代器的大部分/所有功能 这就是我遇到麻烦的地方,代码将无法使用大约2英里的模板相关错误输出进行编译。如果您有兴趣,我可

我开始编写一个类,它的行为很像
std::vector
,但带有一些数学运算。主要是向量的范数和重载重要的数学运算符(+、-等),它们将按元素进行加、减运算

这个类发布在下面,我使用了
boost::operators
来编写所有的数学运算符,它们都能完美地工作。我在实现迭代器时遇到了一些问题。我尝试将迭代器作为嵌套类编写,并使用
boost::iterator
获得
std::vector
迭代器的大部分/所有功能

这就是我遇到麻烦的地方,代码将无法使用大约2英里的模板相关错误输出进行编译。如果您有兴趣,我可以发布,但这是典型的详细boost模板错误

我的问题有两个方面

首先,构图是最好的设计选择吗?我可以用私有继承或装饰图案做得更好吗?或者也许有人知道这个想法在图书馆的实施

第二,迭代器有什么问题?我觉得我在我的
boost::iterator
实现中缺少了一些基本的东西,我想修复它,而不是改变我的设计

我没有在大多数方法中包含实现,因为它们要么微不足道,要么不重要

//publicly inherits important boost::operators classes
template <class T>
class Coords: boost::arithmetic<Coords<T>
            ,boost::arithmetic<Coords<T>, T
//              ,boost::indexable<Coords<T>,int,T&
//              ,boost::dereferenceable<Coords<T>, T*>
//            >
    >
>
{
private:
    //private member
    std::vector<T> x_;
public:

    //public constructors
    Coords(int n, T x): x_(n,x){};
    Coords(int n): x_(n){};
    Coords(std::vector<T> &x);
    Coords(const Coords &rhs);

    //iterator nested class, public inherits boost::iterator_facade
    class iterator: public boost::iterator_facade<iterator, Coords<T>, std::random_access_iterator_tag>{
        private:
            typename std::vector<T>::iterator iter_;

            friend class boost::iterator_core_access;

            void increment() { iter_ = iter_++;};

            void decrement() { iter_ = iter_--;};

            void advance(int n){ iter_ = iter_+=n;};

            void equal(iterator const &other) const{
                return this->iter_ == other.iter_;
            }

            T& dereference() const {return *iter_;};

            int distance_to(iterator const &other) const{
                return this->iter_ - other.iter_;
            }

        public:
            iterator():iter_(0){};

            explicit iterator(const typename Coords<T>::iterator& it):iter_(it.iter_){};

            explicit iterator(const typename std::vector<T>::iterator& it):iter_(it){};

    };

    //methods from std::vector I would like to 'copy'
    typename Coords<T>::iterator begin(){return iterator(x_.begin());};
    typename Coords<T>::iterator end(){return iterator(x_.end());};
    typename Coords<T>::iterator operator[](std::size_t n);
    std::size_t size(){return x.size()};

    //mathematical methods
    T norm() const;
    T square() const;
    T sum() const;
    T dotProd(const Coords &rhs);

    //important operator overloads
    Coords operator+=(const T &rhs);
    Coords operator-=(const T &rhs);
    Coords operator*=(const T &rhs);
    Coords operator/=(const T &rhs);
    Coords operator+=(const Coords<T> &rhs);
    Coords operator-=(const Coords<T> &rhs);
    Coords operator*=(const Coords<T> &rhs);
    Coords operator/=(const Coords<T> &rhs);
};
//公开继承重要的boost::operators类
模板
类坐标:boost::算术
{
私人:
//私人会员
std::向量x_;
公众:
//公共建设者
Coords(intn,tx):x_uz(n,x){};
Coords(int n):x_un(n){};
坐标(std::vector&x);
Coords(const Coords&rhs);
//迭代器嵌套类,public继承boost::iterator\u facade
类迭代器:publicboost::迭代器{
私人:
typename std::vector::iterator iter\ux;
朋友类boost::迭代器\u核心\u访问;
无效增量();
无效减量();
空位推进(int n){iter_uu=iter_u+=n;};
无效相等(迭代器常量和其他)常量{
返回此->iter\u==other.iter\ux;
}
T&dereference()常量{return*iter};
int距离_到(迭代器常数和其他)常数{
返回此->iter\uOther.iter\uOther;
}
公众:
迭代器():iter_0{};
显式迭代器(constTypeName Coords::iterator&it):iter_Uit(it.iter_Ui){};
显式迭代器(consttypename std::vector::iterator&it):iter(it){};
};
//我想“复制”std::vector中的方法
typename Coords::迭代器begin(){返回迭代器(x_uz.begin());};
typename Coords::迭代器end(){return iterator(x_.end());};
typename Coords::迭代器运算符[](std::size\u t n);
std::size\u t size(){return x.size()};
//数学方法
T范数()常数;
T平方()常数;
T sum()常数;
T dotProd(const Coords&rhs);
//重要运算符重载
坐标运算符+=(常数T和rhs);
坐标运算符-=(常数T和rhs);
坐标运算符*=(常数T和rhs);
坐标运算符/=(常数T和rhs);
坐标运算符+=(常数坐标和rhs);
坐标运算符-=(常数坐标和rhs);
坐标运算符*=(常数坐标和rhs);
坐标运算符/=(常数坐标和rhs);
};

这修复了程序的一些问题:

#include <boost/operators.hpp>
#include <boost/iterator.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <vector>
//publicly inherits important boost::operators classes
template <class T>
class Coords: boost::arithmetic<Coords<T>
            ,boost::arithmetic<Coords<T>, T
//              ,boost::indexable<Coords<T>,int,T&
//              ,boost::dereferenceable<Coords<T>, T*>
//            >
    >
>
{
private:
    //private member
    std::vector<T> x_;
public:

    //public constructors
    Coords(int n, T x): x_(n,x){};
    Coords(int n): x_(n){};
    Coords(std::vector<T> &x);
    Coords(const Coords &rhs);

    //iterator nested class, public inherits boost::iterator_facade
    class iterator: public boost::iterator_facade<iterator, T, boost::random_access_traversal_tag >{
        private:
            typename std::vector<T>::iterator iter_;

            friend class boost::iterator_core_access;

            void increment() { ++iter_;}

            void decrement() { --iter_; }

            void advance(int n){ iter_+=n;};

            bool equal(iterator const &other) const{
                return this->iter_ == other.iter_;
            }

            T& dereference() const {return *iter_;};

            int distance_to(iterator const &other) const{
                return this->iter_ - other.iter_;
            }

        public:
            iterator():iter_(0){};

            iterator(const iterator& it):iter_(it.iter_){};
            iterator(iterator& it):iter_(it.iter_){};

            explicit iterator(const typename std::vector<T>::iterator& it):iter_(it){};

    };

    //methods from std::vector I would like to 'copy'
    iterator begin(){return iterator(x_.begin());};
    iterator end(){return iterator(x_.end());};
    iterator operator[](std::size_t n);
    std::size_t size(){return x_.size();};

    //mathematical methods
    T norm() const;
    T square() const;
    T sum() const;
    T dotProd(const Coords &rhs);

    //important operator overloads
    Coords operator+=(const T &rhs);
    Coords operator-=(const T &rhs);
    Coords operator*=(const T &rhs);
    Coords operator/=(const T &rhs);
    Coords operator+=(const Coords<T> &rhs);
    Coords operator-=(const Coords<T> &rhs);
    Coords operator*=(const Coords<T> &rhs);
    Coords operator/=(const Coords<T> &rhs);
};

int main() {
  Coords<int> c(3);
  for(Coords<int>::iterator it(c.begin()); it != c.end(); ++it)
    *it;
}
#包括
#包括
#包括
#包括
//公开继承重要的boost::operators类
模板
类坐标:boost::算术
{
私人:
//私人会员
std::向量x_;
公众:
//公共建设者
Coords(intn,tx):x_uz(n,x){};
Coords(int n):x_un(n){};
坐标(std::vector&x);
Coords(const Coords&rhs);
//迭代器嵌套类,public继承boost::iterator\u facade
类迭代器:publicboost::迭代器{
私人:
typename std::vector::iterator iter\ux;
朋友类boost::迭代器\u核心\u访问;
void increment(){++iter}
无效减量()
空位推进(int n){iter_u+=n;};
布尔相等(迭代器常量和其他)常量{
返回此->iter\u==other.iter\ux;
}
T&dereference()常量{return*iter};
int距离_到(迭代器常数和其他)常数{
返回此->iter\uOther.iter\uOther;
}
公众:
迭代器():iter_0{};
迭代器(const-iterator&it):iter(it.iter){};
迭代器(iterator&it):iter(it.iter){};
显式迭代器(consttypename std::vector::iterator&it):iter(it){};
};
//我想“复制”std::vector中的方法
迭代器begin(){返回迭代器(x_u.begin());};
迭代器end(){return iterator(x_u.end());};
迭代器运算符[](std::size\u t n);
std::size_t size(){返回x_u.size();};
//数学方法
T范数()常数;
T平方()常数;
T sum()常数;
T dotProd(const Coords&rhs);
//重要运算符重载
坐标运算符+=(常数T和rhs);
坐标运算符-=(常数T和rhs);
坐标运算符*=(常数T和rhs);
坐标运算符/=(常数T和rhs);
坐标运算符+=(常数坐标和rhs);
坐标运算符-=(常数坐标和rhs);
坐标运算符*=(常数坐标和rhs);
坐标运算符/=(常数坐标和rhs);
};
int main(){
协调c(3);
for(Coords::iterator it(c.begin());it!=c.end();++it)
*它;
}
  • 似乎是说,
    iterator\u facade
    的第三个模板参数是
    boost:
    标记,而不是
    std:
    标记
  • iterator\u facade的第二个模板参数是值类型,而不是容器类型
  • 递增
    递减
    前进
    的代码都是未定义的(我认为)