2

コードでoperator<<をオーバーロードしようとしています。カスタムクラスで<<演算子を使用しようとしている行をコメントアウトすると、正常にコンパイルされます。エラーは、c ++ライブラリ(?)が好きではないように見えます。

この問題に関する私のすべての研究は、それがリンクの問題であると述べています。ほとんどの場合、gccの代わりにg++を使用することをお勧めします。コンパイラとしてg++を使用していますが、それでもこのエラーが発生します。

コード:

#include <iostream>
using namespace std;

//prototype the class and the functions
template<class T> class strange;
template<class T> ostream& operator<< (ostream& osObject, strange<T>& sObject);


//begin class
template <class T>
class strange
{
    public:
        // .... function prototypes go here.
            strange(T x,T y);
            friend ostream& operator<< <> (ostream& osObject, strange<T>& sObject);

    private:
    T a;
    T b;
};
// .... your function definitions go here
template <class T>
        strange<T>::strange(T first, T second){
        a = first;
        b = second;
}

template <class T>
ostream& operator<< (ostream& osObject, const strange<T>& sObject){
        osObject << sObject.a << ", " << sObject.b;
        return osObject;
}



int main()
{
    strange<int> x1(4,6) , x2(12,2) ;
    //strange<char> y1('m','n') , y2('m','n') ;
    cout << "x1 = " << x1 << endl;
    return 0;
}

エラー:

test.cpp:(.text+0x7a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, strange<int>&)'
collect2: ld returned 1 exit status

何がこれを引き起こしているのか考えていますか?

4

1 に答える 1

4

2つの変更を行いました。1つはフレンド定義に、もう1つはプロトタイプに変更を加えました。これはコンパイルする必要があります:

#include <iostream>
using namespace std;

//prototype the class and the functions
template<class T> class strange;
template<class T> ostream& operator<< (ostream& osObject, const strange<T>& sObject);


//begin class
template <class T>
class strange
{
    public:
        // .... function prototypes go here.
            strange(T x,T y);
            friend ostream& operator<< <> (ostream& osObject, const strange<T>& sObject);

    private:
    T a;
    T b;
};
// .... your function definitions go here
template <class T>
        strange<T>::strange(T first, T second){
        a = first;
        b = second;
}

template <class T>
ostream& operator<< (ostream& osObject, const strange<T>& sObject){
        osObject << sObject.a << ", " << sObject.b;
        return osObject;
}



int main()
{
    strange<int> x1(4,6) , x2(12,2) ;
    //strange<char> y1('m','n') , y2('m','n') ;
    cout << "x1 = " << x1 << endl;
    return 0;
}

そしてこれはclang、g ++、そしてideoneでコンパイルされます

この問題を説明するために、コンパイラーは以下の定義のリンク時間を調べています。

 std::ostream & operator<< <int>(std::ostream &, strange<int>&);

次の定義しかない場合:

 std::ostream & operator<< <int>(std::ostream &, strange<int> const &);

これは、プロトタイプ(明示的なものと友人の両方)と定義の間の誤解が原因です。

于 2012-09-08T15:47:38.700 に答える