Newsgroup: it.comp.lang.c++
Titolo: Programma di prova!

Ciao a tutti! Avrei un problema al programma che ora vi posto.
#include <iostream> 
#include <cstring>
using namespace std; 
 
const int maxQsize = 100; 
class Sample {
  int id;
public:
  Sample() {id=0;}
  Sample(int x) {id=x;}
  void show() {cout<<id<<endl;}
};
class QExcpt {
public:
    char msg[80];
};
 
// Crea una classe Queue generica.
template <class QType> class Queue {
  QType q[maxQsize]; // questo array contiene la coda
  int size; // il numero massimo di elementi 
             // che la coda può accogliere
  int putloc, getloc; // gli indici put e get
  QExcpt Qerr;
public:
  // Costruisca una coda di lunghezza specifica.
  Queue(int len) {
  // La coda dev'essere meno di max e positiva.
  if(len > maxQsize) len = maxQsize;
    else if(len <= 0) len = 1; 
    size = len;
    putloc = getloc = 0;
  }
  // Inserisce dati nella coda.
  void put(QType data) {
    if(putloc == size) {
      strcpy(Qerr.msg, "La coda è piena\n");
      throw Qerr;
    }
 
    putloc++;
    q[putloc] = data;
  }
  // Estrae dati dalla coda.
  QType get() {
    if(getloc == putloc) {
      strcpy(Qerr.msg, "La coda è vuota\n");
      throw Qerr;
    }
 
    getloc++;
    return q[getloc];
  }
};
// Dimostrazione della classe Queue generica.
int main()
{
  Queue<int> iQa(10), iQb(10);  // crea due code di interi
  try{
  iQa.put(1);
  iQa.put(2);
  iQa.put(3);
  iQb.put(10);
  iQb.put(20);
  iQb.put(30);
  cout << "Contenuto della coda di interi iQa: ";
  for(int i=0; i < 3; i++)
    cout << iQa.get() << " ";
  cout << endl;
  cout << "Contenuto della coda di interi iQb: ";
  for(int i=0; i < 3; i++)
    cout << iQb.get() << " ";
    cout << endl;
  Queue<double> dQa(10), dQb(10);  // crea due code di double
  dQa.put(1.01);
  dQa.put(2.02);
  dQa.put(3.03);
  dQb.put(10.01);
  dQb.put(20.02);  
  dQb.put(30.03);
  cout << "Contenuto della coda di double dQa: ";
  for(int i=0; i < 3; i++)
    cout << dQa.get() << " ";
  cout << endl;
  cout << "Contenuto della coda di double dQb: "; 
  for(int i=0; i < 3; i++) 
    cout << dQb.get() << " "; 
  cout << endl; 
  Queue<Sample> sQa(3); // crea una coda di oggetti Sample
  Sample ob1, ob2=1, ob3=3;
  sQa.put(ob1);
  sQa.put(ob2);
  sQa.put(ob3);
 
  for(int i=0; i<3; i++) 
    cout<<sQa.get().show()<<" "; // Errore!!!!!!
  cout<<endl;
  }
  catch(QExcpt exc){
    cout<<exc.msg;
  }
 
  return 0; 
}
Praticamente alla line con commento Errore!!!! ottengo il seguente errore:
error: no match for 'operator<<' in 'std::cout <<
   (&Queue<QType>::get() [with QType = Sample]())->Sample::show()'
/usr/include/c++/3.3/bits/ostream.tcc:63: error: candidates are:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(std::basic_ostream<_CharT,
   _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
   _Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:74: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(std::basic_ios<_CharT,
   _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits
   = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:86: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char,
   _Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:122: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:156: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:98: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(bool) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/ostream:178: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(short int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/ostream:189: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/ostream:193: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/ostream:204: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:181: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long long int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:216: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:241: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(double) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/ostream:219: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(float) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:265: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(long double) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:289: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(const void*) [with _CharT = char, _Traits =
   std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:313: error:
   std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
   _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT =
   char, _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:436: error:
   std::basic_ostream<_CharT, _Traits>&
   std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT =
   char, _Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:512: error:
   std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
   _Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:447: error:
   std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
   _Traits>&, signed char) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:452: error:
   std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
   _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:572: error:
   std::basic_ostream<_CharT, _Traits>&
   std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with
   _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:616: error:
   std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
   _Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:486: error:
   std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
   _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:491: error:
   std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
   _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]
Non sono riuscito a capire il perchè. Qualcuno può aiutarmi a capire? 
Grazie (scusate il post lungo),
MAX

 cout<<sQa.get().show()<<" "; // Errore!!!!!!
Poiché 'show' restituisce 'void', stai cercando di passare un 'void'
all'operatore <<, che non ha senso. Puoi sempliciemente scrivere:
  sQa.get().show(); cout << " ";
...oppure (meglio):
Cambia 'show', in questo modo:
 void show( std::ostream& out ) const { out<<id<<endl; }
Poi aggiungi:
 std::ostream& operator<<( std::ostream& out, const Sample& n )
 {   n.show(out); return out;
 }
Quindi riscrivi la riga citata così:
 cout << sQa.get() <<" "; 
Max



Grazie per le soluzioni, entrambe funzionanti. La seconda mi ha dato la
possibilità di vedere l'overload dell'operatore << in azione, quindi
grazie del suggerimento. Scusa per la risposta tardiva...
Ciao, MAX