friend in Templates?



  • #include <iostream>
    using namespace std;
    
    template <typename T = int>
    class Foo
    {
    public:
    	Foo(T value)
    	{
    		this->value = value;
    	}
    
    	friend ostream &operator<<(ostream &out, const Foo<T> &o);
    private:
    	T value;
    };
    
    template <typename T>
    ostream &operator<<(ostream &out, const Foo<T> &o)
    {
    	out<<o.value;
    };
    
    int main()
    {
    	cout<<"Hallo Welt!"<<endl;
    
    	Foo<> t(2);
    	cout<<t<<endl;
    
    	system("pause");
    }
    

    was mache ich hier falsch?

    Compiler Ausgabe:
    Nicht aufgelöstes externes Symbol '"class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Foo<int> const &)" (??6@YAAAV?basic_ostream@DU?basic\_ostream@DU?char_traits@D@std@@@std@@AAV01@ABV?$Foo@H@@@Z)', verwiesen in Funktion '_main'
    Debug/Minimal.exe : fatal error LNK1120: 1 unaufgelöste externe Verweise

    kann ein Template etwa keine "friends" besitzen?



  • #include <iostream>
    using namespace std;
    
    template <class T >
    class Foo
    {
    public:
        Foo(T value)
        {
            this->value = value;
        }
    
        friend ostream &operator<<(ostream &out, const Foo<T> &o);
    private:
        T value;
    };
    
    template <class T>
    ostream &operator<<(ostream &out, const Foo<T> &o)
    {
        out<<o.value;
    };
    
    int main()
    {
        cout<<"Hallo Welt!"<<endl;
    
        Foo <int> t(2);
        cout<<t<<endl;
    
        system("pause");
    }
    


  • aus irgendwelchen gründen hast du typename durch class ersetzt und meine Defaultkonstruktion als int vernichtet, was dem Problem aber auch keine Abhilfe schaft



  • Vertexwahn schrieb:

    was mache ich hier falsch?

    Du verschweigst bei der friend-Deklaration, dass der operator<< ein Template ist. Ein Rückgabewert wäre auch noch nett 😉



  • Rückgabewert? es wird eine Referenz auf ostream zurückgegeben - wieso fehlt hier ein rückgabetyp? und wie sage ich dem Compiler das der friend ein Template ist? einfach template <typename T> davor oder wie?



  • #include <iostream> 
    using namespace std; 
    
    template <class T > 
    class Foo 
    { 
    public: 
        Foo(T value) 
        { 
            this->value = value; 
        } 
    
    	template <class T>
        friend ostream &operator<<(ostream &out, const Foo<T> &o); 
    private: 
        T value; 
    }; 
    
    template <class T> 
    ostream &operator<<(ostream &out, const Foo<T> &o) 
    { 
        out<<o.value; 
    	return out;
    }; 
    
    int main() 
    { 
        cout<<"Hallo Welt!"<<endl; 
    
        Foo <int> t(2); 
        cout<<t<<endl; 
    
        system("pause"); 
    }
    

    passt das so?



  • aso, srry es liegt eher daran das der "<<" operator nicht friend compatible ist. Das ist doch ne schift operation, die die werte in deine Klasse "lädt". *aaaaaahhhglatteis*



  • Vertexwahn schrieb:

    Rückgabewert? es wird eine Referenz auf ostream zurückgegeben - wieso fehlt hier ein rückgabetyp?

    Es fehlte der Rückgabewert (d.h. die return-Anweisung), aber das hast du ja offenbar schon gemerkt 😉

    Vertexwahn schrieb:

    passt das so?

    Ja, sieht gut aus. Warum hast du den Default-Templateparameter jetzt selbst entfernt?

    Ich würde eher eine Initialisierungsliste im Konstruktor verwenden.

    mosta schrieb:

    aso, srry es liegt eher daran das der "<<" operator nicht friend compatible ist. Das ist doch ne schift operation, die die werte in deine Klasse "lädt". *aaaaaahhhglatteis*

    Das ist kompletter Unsinn. Erst mal 'ne Tasse Kaffee?


Anmelden zum Antworten