我試圖專注< <運營商對於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){
我能做些什麼來重載或專門化該操作符的字符?
查看這兩個線程: [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