C++ 重新定义二进制运算符+;带参数类

C++ 重新定义二进制运算符+;带参数类,c++,c++11,C++,C++11,我有我的类Polynome,但当我想重新定义+运算符时,我有一些问题 #ifndef __POLYNOME_HPP__ #define __POLYNOME_HPP__ #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; template<class T> class Polynome { priv

我有我的类Polynome,但当我想重新定义+运算符时,我有一些问题

#ifndef __POLYNOME_HPP__
#define __POLYNOME_HPP__

#include <iostream>
#include <map>
#include <set>
#include <math.h>
using namespace std;

template<class T>
class Polynome {

    private :
        map<int, T> _coef;
    public :
        Polynome<T>(): _coef() {

        }

        Polynome<T>(Polynome<T>& pol):_coef(pol._coef) {

        }

        Polynome<T>(const T arrayOfT[]) {
            for(int i = 0; i < sizeof(arrayOfT)/sizeof(arrayOfT[0]); i++) {
                _coef.insert(pair<int, T>(i, arrayOfT[i]));
            }
        }

        Polynome<T>(const set< pair<int, T> > s) {
            typename set< pair<int,T> >::iterator it;
            for(it = s.begin(); it!=s.end(); ++it) {
                _coef.insert(*it);
            }
        }

        friend ostream& operator<<(ostream& os, Polynome pol) {
            typename map<int,T>::iterator it;
            for (it=pol._coef.begin(); it!=pol._coef.end(); ++it) {
                if(it->first != 0) {
                    os << it->second << "x^" << it->first << " + ";
                }
            }
            os << " 0 " << endl;
            return os;
        }

        T evaluation(int x) {
            T result =0;

            typename map<int, T>::iterator it;

            for(it = _coef.begin(); it != _coef.end(); ++it) {


                result += pow(it->second, it->first) * x;

            }
            return result;
        }

        T& operator[](int indice) {
            return _coef[indice];
        }

        Polynome<T> derivatative() const {
            Polynome derivativePol;

            typename map<int, T>::iterator it;
            for(it = _coef.begin(); it != _coef.end(); ++it) {
                if(it->frist >= 1) {
                    pair<int, T> derivativePair = pair<int, T>(it->first - 1, it->first * it->second);
                    derivativePol.insert(derivativePair);
                }
            }

            return derivativePol;
        }

        friend Polynome<T> operator+(Polynome<T>& p, Polynome<T>& p2) {
            Polynome<T> pResult(p);
            typename map<int, T>::iterator it;
            for(it = p._coef.begin(); it != p._coef.end(); ++it) {

                pair<int, T> newPair = pair<int, T>(it->first, it->second + p2[it->first]);
                pResult._coef.insert(newPair);

            }

            return pResult;
        }
};
#endif
\ifndef\uuu多项式\uu水电站__
#定义多项式HPP__
#包括
#包括
#包括
#包括
使用名称空间std;
模板
类多项式{
私人:
map_coef;
公众:
多项式():_coef(){
}
多项式(Polynome&pol):\u coef(pol.\u coef){
}
多项式(常数T arrayOfT[]){
对于(int i=0;is){
typename set::迭代器;
for(it=s.begin();it!=s.end();+it){
_系数插入(*it);
}
}
friend ostream&operatorfirst-1,it->first*it->second);
derivativePol.插入(derivativePair);
}
}
返回衍生酚;
}
友元多项式算子+(多项式&p,多项式&p2){
多项式预处理(p);
typename映射::迭代器;
for(it=p.coef.begin();it!=p.coef.end();+it){
pair newPair=pair(it->first,it->second+p2[it->first]);
预压系数插入件(新对);
}
回归预设;
}
};
#恩迪夫
编译器给了我以下错误:

Polynome.cpp:32:19:错误:没有用于调用的匹配函数 ‘Polynome::Polynome(Polynome)’

Polynome.cpp:32:19:注:候选项为:包含在 Polynome.cpp:1:0:Polynome.hpp:30:9:注: Polynome::Polynome(std::set>)[带T=int] 多项式(常数集s){ ^Polynome.hpp:30:9:注意:参数1从“Polynome”到“std::set”没有已知的转换, std::less>,std::分配器>

'Polynome.hpp:24:9:注:Polynome::Polynome(常数T*)[带T=int] 多项式(常数T arrayOfT[]){ ^Polynome.hpp:24:9:注:参数1从“Polynome”到“const int*”Polynome没有已知的转换。hpp:20:9:注: Polynome::Polynome(Polynome&)[带T=int] 多项式(Polynome&pol):\u coef(pol.\u coef){ ^Polynome.hpp:20:9:注意:参数1从“Polynome”到“Polynome&”Polynome没有已知的转换。hpp:16:9:注意: Polynome::Polynome()[带T=int] 多项式():_coef(){ ^Polynome.hpp:16:9:注意:候选者需要0个参数,1个提供了Polynome.hpp:37:25:错误:初始化的参数2
'std::ostream&operator您的复制构造函数应该引用
const
对象:

Polynome(const Polynome<T>& pol):_coef(pol._coef) {

    }
多项式(常数多项式和pol):\u coef(pol.\u coef){
}

或者更好的做法是,遵循零规则,根本不声明复制构造函数。

sizeof(arrayOfT)
:令人惊讶的是,
arrayOfT
是一个指针!这不会满足您的要求。但我在堆栈溢出帖子中看到过这一点来获取数组的大小,它是假的吗?@BobReynolds在某些情况下,是的。在其他情况下,不是。
\uuuuuPolynome\uHPP\uUp
是为实现保留的标识符。除非您在编写编译器的标准库时,您的程序格式不正确。很好,这可以正常工作。但现在我想将我的+运算符的签名更改为friend Polynome operator+(const Polynome&p,const Polynome&p2)。它告诉我错误:“operator=”不匹配(操作数类型为'std::mapI在我的第一次编辑中添加了我的先例您需要使用
常量迭代器
,而不仅仅是
迭代器
Polynome(const Polynome<T>& pol):_coef(pol._coef) {

    }