Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [C++] Problema ridefinizione operatore << ostream in classe

    Ciao a tutti,
    cerco di essere breve.
    Ho una classe Node di tipo template di cui voglio ridefinire l'operatore << per poter utilizzare

    codice:
    Node<int>* nodx;
    nodx.value = 20;
    cout<<nodx<<endl;
    Nel file Node.h ho scritto:

    codice:
    #include <iostream>
    
    using namespace std;
    template <typename T>
    class Node 
    {
    	/* Overload operatori di streaming */
    	friend ostream & operator<<(ostream &out, const Node &node) {
            return out<<node.value;
            }
    }
    ma ciò non funziona: in output ho una stringa tipo 0x8d2500.
    Cosa c'è di sbagliato?
    L'aquilone è simbolo che l'uomo vuole andare più su

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381

    Re: [C++] Problema ridefinizione operatore << ostream in classe

    Cosa c'è di sbagliato?
    codice:
    Node<int>* nodx;
    nodx.value = 20;
    cout<<nodx<<endl;
    nodx è un puntatore a un oggetto, non un oggetto. E gli operatori voglio oggetti, non puntatori a oggetti (presumo che nodx.value = 20 sia un errore di battitura dell'esempio).

    Questo è corretto.
    codice:
    Node<int>* nodx;
    nodx->value = 20;
    cout<< (*nodx) <<endl;
    Poi devi ritornare lo ostream.
    codice:
    template <typename T>
    class Node 
    {
    	/* Overload operatori di streaming */
    	friend ostream & operator<<(ostream &out, const Node &node) {
                out<<node.value;
                return out;
            }
    }
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Grazie shodan, i tuoi consigli mi hanno illuminato la mente!
    L'aquilone è simbolo che l'uomo vuole andare più su

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Prego
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.