【问题标题】:specialize friend operator in template class previously defined error在模板类中专门定义友元运算符先前定义的错误
【发布时间】:2015-07-23 10:09:59
【问题描述】:

我试图在我的模板类中专门化

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++ 给我:

在包含的文件中 /home/pawel/ClionProjects/lista9/obliczenia.cpp:1:0: /home/pawel/ClionProjects/lista9/obliczenia.hpp:在实例化 “类 obliczenia::tablicowy”: /home/pawel/ClionProjects/lista9/obliczenia.cpp:38:28:需要来自 这里/home/pawel/ClionProjects/lista9/obliczenia.hpp:40:30:错误: 重新定义‘std::ostream& obliczenia::operator

我可以做些什么来重载或专门化 char 运算符?

【问题讨论】:

标签: c++ templates operator-keyword friend


【解决方案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 示例)

【讨论】:

    【解决方案2】:

    尝试在friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, tablicowy&lt;char&gt;&amp; that ); 之前添加template &lt;&gt; 以表明它是模板专业化

    您还需要将朋友类的实现移到课堂之外 - 详情请参阅 Explicit specialization of friend function for a class template

    【讨论】:

    • 当我添加这个时:错误:非命名空间范围内的显式特化 'class obliczenia::tablicowy' 模板 ^ /home/pawel/ClionProjects/lista9/obliczenia.hpp: In 'class obliczenia::tablicowy'的实例化:
    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2011-05-16
    • 2013-08-19
    • 2016-10-19
    • 2016-06-23
    • 2012-03-08
    相关资源
    最近更新 更多