I tried to specialize << operator for char in my template class
我试图在我的模板类中专门化<< operator for 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++ give me :
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 ){
在/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&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&)'先前在此定义std :: ostream&operator <<(std :: ostream&out,tablicowy&that){
What can i do to overload or specialize that operator for char?
我可以做什么来重载或专门化该运算符的char?
2 个解决方案
#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:
并在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)
#2
Try adding template <>
before friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
to indicate it is a Template Specialization
尝试在朋友std :: ostream&operator < <之前添加模板<> (std :: ostream&out,tablicowy
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
您还需要在类之外移动友元类的实现 - 有关详细信息,请参阅类模板的朋友函数的显式特化
#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:
并在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)
#2
Try adding template <>
before friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
to indicate it is a Template Specialization
尝试在朋友std :: ostream&operator < <之前添加模板<> (std :: ostream&out,tablicowy
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
您还需要在类之外移动友元类的实现 - 有关详细信息,请参阅类模板的朋友函数的显式特化