2015-05-12 66 views
0

我試圖專注< <運營商對於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++給我:

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){

我能做些什麼來重載或專門化該操作符的字符?

+0

查看這兩個線程: [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

回答

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例如)

+0

萬物作品,非常感謝 –

0

嘗試增加template <>friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that);前,表示它是一個模板專門

您還需要移動實現友元類的類外 - 見Explicit specialization of friend function for a class template的細節

+0

當我加入此: 錯誤:顯式特非命名空間範圍 '類obliczenia :: tablicowy ' 模板<> ^ /home/pawel/ClionProjects/lista9/obliczenia.hpp:在實例化'class obliczenia :: tablicowy ': –

+0

檢查http://stackoverflow.com/questions/13464064/explicit-specialization-of-friend-function-for-a-class-template – Hcorg