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

    [c++] prototipi di template con progetti e più file?

    ragazzi ho una semplice funzione template:
    codice:
    template <class T>
    void __debugLog(T msg, int level) {
    	if (level <= DEBUG_LEVEL )
    		cout << "[Debug:" << level << "] " << msg << endl;
    }

    come posso scrivere il prototipo di questa funzione nel mio file header.h?


    riportando un pezzo da: http://www.cplusplus.com/doc/tutorial/templates/

    Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.
    praticmante dice che la dichirazione e l'implemetnazione di una funzione template devono stare nello stesso posto..

    ma se metto il codice nel mio file header.h il mio prof mi uccide all'istante credo

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Se conosce i template non lo farà.
    Al limite metti:
    [CODE]
    template <class T>
    inline void quel che è.
    ( E' richiesto dai linker moderni. )
    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
    rischio l'esame cosi....

  4. #4
    Il punto è che non ci sono alternative, a meno che il tuo professore non disponga dell'unico compilatore in commercio che implementa correttamente la parola chiave export.
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Non rischi niente. E' obbligatorio mettere i template nei file header. Lo richiede il linguaggio. Solo se come compilatore usi il Comeau puoi separare dichiarazione e definizione, (e non è del tutto vero nemmeno quello comunque).

    Edit:
    Da qualche parte ho letto che nel caso del Comeau, occorre sempre portarsi dietro il sorgente del template, quindi nemmeno lui supporta pienamente l'export richiesto dallo standard.
    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
    vabene... speriamo che il mio prof lo sappia....


    ho messo anche la key inline come mi hai detto tu (anche se senza il visual c++2008 comunque compilava senza warning)

  7. #7
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Finchè lo usi nella stessa unità di compilazione, la mancanza di inline non genera errori di linking (non warning), in più unità si. Me ne sono accorto quando sono diventato mezzo matto a capire perché del codice che compilava benissimo con Borland 5.5.1, faceva protestare VC++
    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.

  8. #8
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Originariamente inviato da xnavigator
    vabene... speriamo che il mio prof lo sappia....
    Se non lo sa, digli di vedere qui;
    http://www.parashift.com/c++-faq-lit...html#faq-35.12
    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.