Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c++]template problema compilatore?

    ciao.
    ho il seguente codice:
    codice:
    #include "stdafx.h"
    #include <vector>
    #include <iostream>
    #include <string>
    using namespace std;
    // As long as things are simple,
    // this approach works fine:
    template<typename C>
    void print1(C& c) {
    typename C::iterator it;
    	for(it = c.begin(); it != c.end(); it++)
    		cout << *it << " ";
    		cout << endl;
    }
    // Template-template argument must
    // be a class; cannot use typename:
    template<typename T, template<typename> class C>
    
    void print2(C<T>& c) {
    	copy(c.begin(), c.end(),ostream_iterator<T>(cout, " "));
    	cout << endl;
    }
    int main() {
    	vector<string> v(5, "Yow!");
    	print1(v);
    	print2(v);
    }
    su print2(v) mi da il seguente errore:

    c:\test\copy constructor\copy\copy.cpp(26): error C2784: 'void print2(C<T> &)' : could not deduce template argument for 'C<__unnamed> &' from 'std::vector<_Ty>'
    with
    [
    _Ty=std::string
    ]
    è un problema di compilatore?
    uso vs 2003.net
    come si trasforma la dichiarazione con deduzione(si chiama cosi?) in dichiarazione esplicita?
    grazie.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Non ho mai visto quel tipo di dichiarazione per una funzione, anche perché le funzioni Template non accettano parametri di default. Dove hai preso quel codice?
    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
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    ciao Shodan.
    Ho preso il codice da thinking c++ cap."Template-templates" pg 134.
    Come sarebbe la versione corretta?
    Grazie.

  4. #4
    da me compila:
    codice:
    /*
    
    $ g++ --version; g++ -Wall -ansi -pedantic -Wextra -Wconversion main.cpp; ./a.out 
    i686-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
    Copyright (C) 2005 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    Yow! Yow! Yow! Yow! Yow! 
    Yow! Yow! Yow! Yow! Yow! 
    
    */
    
    // #include "stdafx.h"
    #include <vector>
    #include <iostream>
    #include <string>
    #include <iterator>
    using namespace std;
    // As long as things are simple,
    // this approach works fine:
    template<typename C>
    void print1(C& c) {
    typename C::iterator it;
    	for(it = c.begin(); it != c.end(); it++)
    		cout << *it << " ";
    		cout << endl;
    }
    // Template-template argument must
    // be a class; cannot use typename:
    template<typename T, template<typename> class C>
    
    void print2(C<T>& c) {
    	copy(c.begin(), c.end(),ostream_iterator<T>(cout, " "));
    	cout << endl;
    }
    int main() {
    	vector<string> v(5, "Yow!");
    	print1(v);
    	print2(v);
    }

  5. #5
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Fa attenzione che li non si parla di funzioni template, ma di classi.
    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.

  6. #6
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    allora puo essere il compilatore mcapp?
    stasera lo provo sul vs 2008.
    Ho provato anche a cercare di dare una definizione esplicita della seconda funzione(leggendo l'errore potrebbe essere quello) per "aggirare" il problema ma non ci riesco.

    Scusate se parlo da profano e chiedo qui, ma mi sto avvicinando piano piano alla programmazione generica e ho letto che anch essa utilizza il polimorfismo (tramite i tipi pero')ed è una programmazione alternativa all oop , forse mi sbaglio ma non è oop anche essa?ovvero esistono le classi , l'ereditarieta ecc...
    Qello che mi chiedo è : quando se devo iniziare un progetto un po esteso utilizzare l'una o utilizzare l'altra?
    e tutti i pattern ad es della gof sono applicabili anche ai template?


    grazie.

  7. #7
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    L'esempio compila con G++ ( come dimostrato), ma con VC++ lo stesso prototipo da errore.
    Il che, onestamente, non mi stupisce dato che i template sono il tallone d'achille di molti compilatori. Per compilare con VC++ il prototipo dev'essere modificato come segue ( come lo stesso Thinking in C++ riporta)
    codice:
    template<typename T, template< typename U, class = allocator<U> > class C>
    void print2(C<T>& c) {
    	copy(c.begin(), c.end(),ostream_iterator<T>(cout, " "));
    	cout << endl;
    };
    ed è una programmazione alternativa all oop
    Direi più che è complementare. Con i template si ha una oop a compile time, con le derivazioni si ha la oop a run time. Sono cose diverse. Tempo fa avevi chiesto consiglio su una abstract factory: quello è un esempio di come il polimorfismo basato su template si integri con quello a run time.
    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 © 2025 vBulletin Solutions, Inc. All rights reserved.