0

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?

  • See these two threads: [http://stackoverflow.com/questions/5949992/template-class-member-function-only-specialization][1] [http://stackoverflow.com/questions/9330561/if-i-want-to-specialise-just-one-method-in-a-template-how-do-i-do-it/9330636#9330636][2] [1]: http://stackoverflow.com/questions/5949992/template-class-member-function-only-specialization [2]: http://stackoverflow.com/questions/9330561/if-i-want-to-specialise-just-one-method-in-a-template-how-do-i-do-it/9330636#9330636 – dna8675 May 12 '15 at 14:28

2 Answers2

1

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)

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

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

Community
  • 1
  • 1
Hcorg
  • 11,598
  • 3
  • 31
  • 36
  • when i added this: error: explicit specialization in non-namespace scope ‘class obliczenia::tablicowy’ template <> ^ /home/pawel/ClionProjects/lista9/obliczenia.hpp: In instantiation of ‘class obliczenia::tablicowy’: – Paweł Szymański May 12 '15 at 14:16
  • check http://stackoverflow.com/questions/13464064/explicit-specialization-of-friend-function-for-a-class-template – Hcorg May 12 '15 at 14:20