在模板类中专门设置friend运算符之前定义的错误

时间:2015-05-12 13:34:41

标签: c++ templates operator-keyword friend

我试图专门化<<我的模板类中的char运算符

HPP

template<class T>
class tablicowy{
public:
    T * tablica;
    int rozmiar;
public:
    tablicowy(T arr[], int n){
        {
            tablica = arr;
            rozmiar = n;
        }
    };
    friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
    friend std::ostream& operator<<(std::ostream& out, tablicowy<T>& that ){
        out << "( ";
        for(int i = 0; i < that.rozmiar; i++){
            out << that.tablica[i] << comma;
        }
        out << ")";
        return out;
    };

};

CPP

std::ostream& operator<<(std::ostream& out, tablicowy<char>& that ){
    out << "'";
    for(int i = 0; i < that.rozmiar; i++){
        out << that.tablica[i];
    }
    out << "'";
    return out;
};

C ++给我:

  

包含来自的文件   /home/pawel/ClionProjects/lista9/obliczenia.cpp:1:0:   /home/pawel/ClionProjects/lista9/obliczenia.hpp:实例化   'class obliczenia :: tablicowy':   /home/pawel/ClionProjects/lista9/obliczenia.cpp:38:28:从   这里/home/pawel/ClionProjects/lista9/obliczenia.hpp:40:30:错误:   重新定义'std :: ostream&amp; obliczenia ::运营商LT;≤(标准:: ostream的&安培;,   obliczenia :: tablicowy&安培)”            朋友std :: ostream&amp; operator&lt;&lt;(std :: ostream&amp; out,tablicowy&amp; that){                                 ^ /home/pawel/ClionProjects/lista9/obliczenia.cpp:36:15:错误:   “的std :: ostream的和放; obliczenia ::运营商LT;≤(标准:: ostream的&安培;,   obliczenia :: tablicowy&amp;)'之前在此定义std :: ostream&amp;   operator&lt;&lt;(std :: ostream&amp; out,tablicowy&amp; that){

如何为char添加或专门化该运算符?

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

// Forward declare the class
template <typename T> class tablicowy;

// Forward declare the template operator
template <typename T>
std::ostream& operator<<(std::ostream& out, tablicowy<T>& that );

// Forward declare the function
std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );

// Your class:
template<class T>
class tablicowy{
public:
    T * tablica;
    int rozmiar;
public:
    tablicowy(T arr[], int n){
        {
            tablica = arr;
            rozmiar = n;
        }
    };
    // just declare them friend.
    friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
    friend std::ostream& operator<< <>(std::ostream& out, tablicowy<T>& that );

};

// Implementation
template <typename T>
std::ostream& operator<<(std::ostream& out, tablicowy<T>& that )
{
    const std::string comma = ",";
    out << "( ";
    for(int i = 0; i < that.rozmiar; i++){
        out << that.tablica[i] << comma;
    }
    out << ")";
    return out;
}

在cpp:

std::ostream& operator<<(std::ostream& out, tablicowy<char>& that ){
    out << "'";
    for(int i = 0; i < that.rozmiar; i++){
        out << that.tablica[i];
    }
    out << "'";
    return out;
}

[https://ideone.com/SXClzp](Live示例)

答案 1 :(得分:0)

尝试在template <>之前添加friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );以表明它是模板专精化

您还需要在课堂外移动朋友类的实现 - 有关详细信息,请参阅Explicit specialization of friend function for a class template