2011-01-09 16 views
6

Ho la seguente classe:Perché questo parametro del modello predefinito non è consentito?

template <typename Type = void> 
class AlignedMemory { 
public: 
    AlignedMemory(size_t alignment, size_t size) 
     : memptr_(0) { 
     int iret(posix_memalign((void **)&memptr_, alignment, size)); 
     if (iret) throw system_error("posix_memalign"); 
    } 
    virtual ~AlignedMemory() { 
     free(memptr_); 
    } 
    operator Type *() const { return memptr_; } 
    Type *operator->() const { return memptr_; } 
    //operator Type &() { return *memptr_; } 
    //Type &operator[](size_t index) const; 
private: 
    Type *memptr_; 
}; 

e si tenta di creare un'istanza di una variabile automatica come questo:

AlignedMemory blah(512, 512); 

Questo dà il seguente errore:

src/cpfs/entry.cpp:438: error: missing template arguments before ‘blah’

Che cosa sto facendo di sbagliato ? void non è un parametro predefinito consentito?

+0

Avete qualche codice che include l'identificatore 'buf' ovunque? –

+1

@Charles: 'buf' è un errore di battitura. Vedi questo: http://www.ideone.com/32gVl ... qualcosa manca alla mente. : P – Nawaz

risposta

11

penso che è necessario scrivere:

AlignedMemory<> blah(512, 512); 

Vedere 14.3 [temp.arg]/4:

When default template-arguments are used, a template-argument list can be empty. In that case the empty <> brackets shall still be used as the template-argument-list.

5

tua sintassi è sbagliata:

AlignedMemory blah(512, 512); //wrong syntax 

sintassi corretta is this:

AlignedMemory<> blah(512, 512); //this uses "void" as default type! 

Il messaggio di errore si dà questo suggerimento. Guardate di nuovo:

src/cpfs/entry.cpp:438: error: missing template arguments before ‘buf’

PS: Sono sicuro che 'buf' è un errore di battitura. Volevi scrivere 'blah', il nome della tua variabile!

Problemi correlati