C++ c+中类错误的重新定义+;

C++ c+中类错误的重新定义+;,c++,numerical-methods,C++,Numerical Methods,我用同样的错误搜索了其他一些页面,但是我的代码没有我能找到的任何问题 我在quadrature.h中定义了一个名为QBase的基类: #ifndef SRC_QUADRATURE_H_ #define SRC_QUADRATURE_H_ #include "enum_order.h" #include "enum_quadrature_type.h" #include <vector> #include <memory> class QBase { prote

我用同样的错误搜索了其他一些页面,但是我的代码没有我能找到的任何问题

我在quadrature.h中定义了一个名为QBase的基类:

#ifndef SRC_QUADRATURE_H_
#define SRC_QUADRATURE_H_

#include "enum_order.h"
#include "enum_quadrature_type.h"
#include <vector>
#include <memory>

class QBase
{
    protected:

        QBase (const Order _order=INVALID_ORDER);

    public:

        virtual ~QBase() {}

        virtual QuadratureType type() const = 0;

        static std::unique_ptr<QBase> build (const QuadratureType qt, const Order order=INVALID_ORDER);

        const std::vector<double> & get_points() const { return _points; }
        const std::vector<double> & get_weights() const { return _weights; }
        std::vector<double> & get_points() { return _points; }
        std::vector<double> & get_weights() { return _weights; }

    protected:

        const Order _order;

        std::vector<double> _points;
        std::vector<double> _weights;
};

#endif /* SRC_QUADRATURE_H */
在主文件中,我使用build()成员函数获取点和权重,如下所示

const Order order = ddp.order;
const QuadratureType qt = ddp.qt;

static std::unique_ptr<QBase> qr(QBase::build(qt,order));

const std::vector<double>& points = qr->get_points();
const std::vector<double>& weights = qr->get_weights();
编译最后一个文件时,出现以下错误:

/home/matteo/flux/gauss_legendre.cxx:13:1: 
error: redefinition of ‘QGaussLegendre::QGaussLegendre(qenum::Order)’
 QGaussLegendre::QGaussLegendre(const Order order)
 ^~~~~~~~~~~~~~
In file included from /home/matteo/flux/gauss_legendre.cxx:8:0:
/home/matteo/flux/gauss_legendre.h:25:3: 
note: ‘QGaussLegendre::QGaussLegendre(qenum::Order)’ previously 
defined here
 QGaussLegendre (const Order _order=INVALID_ORDER) : QBase (_order)
 ^~~~~~~~~~~~~~
我能做些什么来解决这个问题?非常感谢

类错误的重新定义

这不是重新定义类的错误。这是一个关于重新定义函数的错误。特别是,函数
QGaussLegendre::QGaussLegendre(const Order Order)
的重新定义,它是类
QGaussLegendre
的构造函数

您首先在
quadrature.h中定义了它:

QGaussLegendre (const Order _order=INVALID_ORDER) : QBase (_order){}
第二次在legendre_gauss.cxx中:

QGaussLegendre::QGaussLegendre(const Order order)
{
我能做些什么来解决这个问题


解决方案是只定义一次函数。

好的,它以前在这里定义过。只定义一次,问题就解决了。正如错误消息所说,您已经定义了两次
QGaussLegendre::QGaussLegendre
。删除其中一个,最好是gauss_legendre.h:25中的空的一个。你读到错误了吗?您正在重新定义的构造函数。
QGaussLegendre
。头文件和.cpp文件都包含它的定义。以两种不同的方式定义,我猜这两种定义需要在cpp文件中组合。因此,我只能在legendre_gauss.cxx中定义构造函数。但是在这个例子中我得到了@Matteo你在类定义中声明了构造函数吗?在gauss_legendre.h中我写@Matteo所以,你声明了函数
QGaussLegendre()
,但是你没有声明
QGaussLegendre(Order)
,这就是你试图定义的?这是你的问题。
QGaussLegendre (const Order _order=INVALID_ORDER) : QBase (_order){}
QGaussLegendre::QGaussLegendre(const Order order)
{