2011-07-26 14 views
2

Boost数学ライブラリの多項式クラスはBoost polynomial classです。私は、新しい機能を追加することにより、このクラスの能力を展開したいと次のように私は、継承を使用します。演算子のあいまいなオーバーロード+

#ifndef POLY_HPP 
#define POLY_HPP 

#include <boost/math/tools/polynomial.hpp> 

template <class T> 
class Poly : public boost::math::tools::polynomial<T>{ 
public: 

    Poly(const T* data, unsigned order) : boost::math::tools::polynomial<T>(data, order){ 

    } 
}; 

#endif 

は、今私は、このクラスの2つのオブジェクトを宣言し、私はそれらを追加したい:

int a[3] = {2, 1, 3}; 
    Poly<int> poly(a, 2); 
    int b[2] = {3, 1}; 
    Poly<int> poly2(b, 1); 
    std::cout << (poly + poly2) << std::endl; 

しかし、そこに合併中のエラーです。

main.cpp: In function ‘int main()’: 
main.cpp:28:26: error: ambiguous overload for ‘operator+’ in ‘poly + poly2’ 
/usr/local/include/boost/math/tools/polynomial.hpp:280:22: note: candidates are: boost::math::tools::polynomial<T> boost::math::tools::operator+(const U&, const boost::math::tools::polynomial<T>&) [with U = Poly<int>, T = int] 
/usr/local/include/boost/math/tools/polynomial.hpp:256:22: note:     boost::math::tools::polynomial<T> boost::math::tools::operator+(const boost::math::tools::polynomial<T>&, const U&) [with T = int, U = Poly<int>] 
/usr/local/include/boost/math/tools/polynomial.hpp:232:22: note:     boost::math::tools::polynomial<T> boost::math::tools::operator+(const boost::math::tools::polynomial<T>&, const boost::math::tools::polynomial<T>&) [with T = int] 
make[2]: Leaving 

operator +には3つのオーバーロード関数が定義されています。私はそれが取るべきと思った:ポリクラスはブースト多項式から継承し、引数が最善を渡している

boost::math::tools::polynomial<T> boost::math::tools::operator+(const boost::math::tools::polynomial<T>&, const boost::math::tools::polynomial<T>&) 

ので、しかし、それは起こりません。演算子+の明示的な新しい定義なしで2つのPolyクラスオブジェクトを追加するには?

+0

'polynomial 'に仮想デストラクタがありますか?私はそうとは思わないので、それからの公的継承は本当に危険です –

答えて

2

これは、私の知る限りでは可能ではない、あなたは(あなたのクラスでboost::math::tools::polynomial<T>からPoly<T>に適切な変換コンストラクタを必要とする...)の呼び出しを明確にするために

template <class T> 
Poly<T> operator + (const Poly<T>& a, const Poly<T>& b) { 
    return Poly<T>(static_cast< boost::math::tools::polynomial<T> >(a) + 
        static_cast< boost::math::tools::polynomial<T> >(b)); 
} 

のようなものが必要になります

関連する問題