specialize friend operator in template class previously defined 错误
specialize friend operator in template class previously defined error
我试图在我的模板中为 char 专门化 << 运算符 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++给我:
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 ){
我可以做些什么来为 char 重载或专门化该运算符?
尝试在 friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
之前添加 template <>
以表明它是模板专业化
您还需要将朋友 class 的实现移到 class 之外 - 有关详细信息,请参阅 Explicit specialization of friend function for a class template
您可以使用以下内容:
// 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;
}
我试图在我的模板中为 char 专门化 << 运算符 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++给我:
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 ){
我可以做些什么来为 char 重载或专门化该运算符?
尝试在 friend std::ostream& operator<<(std::ostream& out, tablicowy<char>& that );
之前添加 template <>
以表明它是模板专业化
您还需要将朋友 class 的实现移到 class 之外 - 有关详细信息,请参阅 Explicit specialization of friend function for a class template
您可以使用以下内容:
// 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;
}