Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;_C++ - Fatal编程技术网

C++ C++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;

C++ C++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;,c++,C++,C++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;';}和';Poly';) 最近开始学习C++。现在我正试图超载,看起来你错过了一个声明。Addstd::ostream&operator很可能是您的代码缺少operator@NeilGatenby只有当您需要访问私人成员时,才

C++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;';}和';Poly';)
最近开始学习C++。现在我正试图超载,看起来你错过了一个声明。Add
std::ostream&operator很可能是您的代码缺少
operator@NeilGatenby只有当您需要访问私人成员时,才需要使用
friend
。在这种情况下,
friend
声明也可以作为常规声明使用(即对世界说“某处有这样一个函数”),因此它解决了这个问题。但只有一份定期声明也应该有效。
class Poly {
public:
    Poly(unsigned int n);
    Poly(Poly &obj);
    int& operator[] (int index);
    unsigned int degree();
    long eval(int x);
    Poly& operator= (Poly& p);


private:
    int *coeffs;
    unsigned int n;
};`
std::ostream& operator << (std::ostream &os, Poly &p) {
    for (int i = 0; i < p.degree()+1; i++){
        if (i != 0)
            os << " + " << p[i] << "x^" << i;
        else
            os << p[i];
    }
    os << std::endl;
    return os;
}
int main() {
    Poly *p1 = new Poly(2);
    (*p1)[0] = 1;
    (*p1)[1] = 1;
    (*p1)[2] = 3;
    cout<< p1->eval(2) << endl;
    Poly p2(2);
    p2 = (*p1);

    cout << p2;

    return 0;
}