简体   繁体   中英

specialize friend operator in template class previously defined error

I tried to specialize << operator for char in my template class

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++ give me :

In file included from /home/pawel/ClionProjects/lista9/obliczenia.cpp:1:0: /home/pawel/ClionProjects/lista9/obliczenia.hpp: In instantiation of 'class obliczenia::tablicowy': /home/pawel/ClionProjects/lista9/obliczenia.cpp:38:28: required from here /home/pawel/ClionProjects/lista9/obliczenia.hpp:40:30: error: redefinition of 'std::ostream& obliczenia::operator<<(std::ostream&, obliczenia::tablicowy&)' friend std::ostream& operator<<(std::ostream& out, tablicowy& that ){ ^ /home/pawel/ClionProjects/lista9/obliczenia.cpp:36:15: error: 'std::ostream& obliczenia::operator<<(std::ostream&, obliczenia::tablicowy&)' previously defined here std::ostream& operator<<(std::ostream& out, tablicowy& that ){

What can i do to overload or specialize that operator for char?

You may use the following:

// 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;
}

And in 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 example)

Try adding template <> before friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that ); to indicate it is a Template Specialization

You will also need to move implementation of friend class outside of class - see Explicit specialization of friend function for a class template for details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM