operator<<(ostream&, const BigUnsigned<I>&) 必须只有一个参数

operator<<(ostream&, const BigUnsigned<I>&) must take exactly one argument

我试图将模板化成员函数的声明和定义分开 class,但最终出现以下错误和警告。

template <typename I>
class BigUnsigned{
    const size_t cell_size=sizeof(I);
    std::vector<I> _integers;
public:
    BigUnsigned();
    BigUnsigned(I);
    friend std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu);
};

std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu){
    for (auto integer : bu._integers){
        out<<integer<<std::endl;
    }
    return out;
}

../hw06/bigunsigned.h:13:77: warning: friend declaration 'std::ostream& operator<<(std::ostream&, const BigUnsigned&)' declares a non-template function [-Wnon-template-friend] friend std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu); ^ ../hw06/bigunsigned.h:13:77: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) ../hw06/bigunsigned.h:16:51: error: invalid use of template-name 'BigUnsigned' without an argument list std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu){ ^ ../hw06/bigunsigned.h: In function 'std::ostream& operator<<(std::ostream&, const int&)': ../hw06/bigunsigned.h:17:28: error: request for member '_integers' in 'bu', which is of non-class type 'const int' for (auto integer : bu._integers){ ^

当我像这样加入声明和定义时,一切都可以正常编译。

template <typename I>
class BigUnsigned{
    const size_t cell_size=sizeof(I);
    std::vector<I> _integers;
public:
    BigUnsigned();
    BigUnsigned(I);
    friend std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu){
        for (auto integer : bu._integers){
            out<<integer<<std::endl;
        }
        return out;
    }
};

目的是打印成员变量_integers到cout。可能是什么问题?

P.S.: 使用 this question 我免费提供了该功能,但没有帮助。

BigUnsigned 是模板类型所以

std::ostream& operator<<(std::ostream& out, const BigUnsigned& bu)

将无法工作,因为没有 BigUnsigned。您需要将 friend 函数设为模板,这样您就可以获取不同类型的 BigUnsigned<some_type>s.

template <typename I>
class BigUnsigned{
    const size_t cell_size=sizeof(I);
    std::vector<I> _integers;
public:
    BigUnsigned();
    BigUnsigned(I);
    template<typename T>
    friend std::ostream& operator<<(std::ostream& out, const BigUnsigned<T>& bu);
};

template<typename T>
std::ostream& operator<<(std::ostream& out, const BigUnsigned<T>& bu){
    for (auto integer : bu._integers){
        out<<integer<<std::endl;
    }
    return out;
}

第二个示例起作用的原因是因为它是在 class 内部声明的,所以它使用 class 使用的模板类型。

的改进。

对于另一个答案,函数模板的所有实例化都是 class 模板所有实例化的 friends。

operator<< <int>BigUnsigned<int>BigUnsigned<double>friend

operator<< <double>BigUnsigned<double>BigUnsigned<FooBar>friend

您可以稍微更改声明,以便

operator<< <int>BigUnsigned<int>friend 而不是 BigUnsigned<double>.

operator<< <double>BigUnsigned<double>friend,但不是 BigUnsigned<FooBar>

// Forward declaration of the class template.
template <typename I> class BigUnsigned;

// Forward declaration of the function template
template <typename I>
std::ostream& operator<<(std::ostream& out, const BigUnsigned<I>& bu);

// Change the friend-ship declaration in the class template.
template <typename I>
class BigUnsigned{
    const size_t cell_size=sizeof(I);
    std::vector<I> _integers;
public:
    BigUnsigned();
    BigUnsigned(I);

    // Grant friend-ship only to a specific instantiation of the
    // function template.
    friend std::ostream& operator<< <I>(std::ostream& out, const BigUnsigned<I>& bu);
};

要添加第三个变体来稍微提高可读性,就是在 class 中定义 friend 函数:

#include <iostream>

template <typename T>
class Foo {
    int test = 42;

    // Note: 'Foo' inside the class body is basically a shortcut for 'Foo<T>'
    // Below line is identical to: friend std::ostream& operator<< (std::ostream &os, Foo<T> const &foo)
    friend std::ostream& operator<< (std::ostream &os, Foo const &foo) {
        return os << foo.test;
    }
};


int main () {
    Foo<int> foo;
    std::cout << foo << '\n';
}