PDA

View Full Version : [c++] problema con struct


guylmaster
18-12-2010, 22:23
Salve a tutti,
ho il seguente metodo di una classe:


template<class K, class E>
void hash_table<K,E>::insert(const mypair<const K, E>& the_pair)
{
// search the table for a matching element
int b = search(the_pair.first);
// chack if matching element found
if (table[b] == NULL){
// no matching element and table not full
table[b] = new mypair< const K, E > (the_pair);
dsize++;
} else {
// check id duplicate or table full
if (table[b]->first == the_pair.first)
// duplicate, change table[b]->second
table[b]->second = the_pair.second;
else{
// table is full
// throw the exception hash_table_full();
}
}
}



nella seguente riga fa riferimento alla struct mypair:

table[b] = new mypair< const K, E > (the_pair)


La struttura è la seguente:


template<class K, class E>
struct mypair {
// data member
K first;
E second;

// methods
mypair(){}
mypair(mypair<const K,E>& the_pair){
first = the_pair.first;
second = the_pair.second;
}
};


Il compilatore però mi da errore dicendomi:

Type no matching function for call to 'mypair<const std::basic_string<char>, std::basic_string<char> >::mypair(const mypair<const std::basic_string<char>, std::basic_string<char> >&)' hash_table.h /Dizionari_2 line 184 C/C++ Problem

Non riesco a capire cos'è che no gli va a genio. Ho provato anche a cambiare la riga incriminata del metodo in:


table[b] = new mypair< const K, E > (& the_pair)


Ma nulla, da sempre lo stesso errore. Sapreste dirmi in cosa sbaglio? perchè ci sto impazzendo da ore.

Vi ringrazio in anticipo,
Guylmaster.

Don[ITA]
19-12-2010, 08:52
Prova a modificare così:

template<class K, class E>
struct mypair {
// data member
K first;
E second;

// methods
mypair(){}
mypair(const mypair<const K,E>& the_pair){
first = the_pair.first;
second = the_pair.second;
}
};


Cya :D

guylmaster
19-12-2010, 12:08
;33964272']Prova a modificare così:

template<class K, class E>
struct mypair {
// data member
K first;
E second;

// methods
mypair(){}
mypair(const mypair<const K,E>& the_pair){
first = the_pair.first;
second = the_pair.second;
}
};


Cya :D

Eh no, solo la chiave K di mypair è costante, non tutto mypair :fagiano:

Ho parzialmente risolto togliendo tutti i const possibili ed immaginabili da quella funzione di insert, il problema è che ora mi da problemi il linker credo.

Qui il nuovo codice tutto insieme
http://www.ideone.com/hgl1Q

e mi da i seguenti errori:


/home/aLq6T8/ccSpDBXf.o:(.rodata._ZTV10hash_tableISsiE[vtable for hash_table<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>]+0x24): undefined reference to `hash_table<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>::erase(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/aLq6T8/ccSpDBXf.o:(.rodata._ZTV10hash_tableISsiE[vtable for hash_table<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>]+0x28): undefined reference to `hash_table<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>::modify(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int const&)'
collect2: ld returned 1 exit status



Da cosa potrebbero dipendere ora?!?

Don[ITA]
19-12-2010, 14:28
Questo:

mypair(const mypair<const K,E>& the_pair){
first = the_pair.first;
second = the_pair.second;
}

è un copy constructor, perciò li dentro the_pair non deve venir modificata quindi va molto bene che sia const. Oltretutto il primo errore che hai segnalato era dovuto proprio a quello :)

void hash_table<K,E>::insert(const mypair<const K, E>& the_pair)