Chyba ve mne nebo v g++?
Michal Krause
michal na krause.cz
Středa Září 6 21:43:40 CEST 2000
Zdravim,
uz nekolik hodin se peru s nasledujicim problemem: mam program v C++ a v
nem STL list L, ktery obsahuje seznam instanci tridy A. Kazda z nich pak
ma v sobe svuj dalsi list Lb instanci tridy B. K obema seznamum (L i Lb)
by se melo pristupovat pomoci iteratoru. Potiz je v tom, ze pokud
pouziji iterator listu L a pres nej zavolam funkci z instance tridy B,
ktera operuje s listem Lb, nefunguje v teto funkci porovnani s Lb.end().
Kdyz udelam totez, ovsem nebudu se na B odkazovat iteratorem, alebrz
primo, vse funguje jak ma.
No, zni to dost divoce, tak snad radeji demonstracni priklad:
------- snip
#include <iostream>
#include <list>
#include <string>
class Test {
public:
list<string> sList;
list<string>::iterator sIter;
void addToList(string s) { sList.push_back(s); }
string &getNext();
};
string &Test::getNext()
{
sIter++;
if (sIter == sList.end()) {
cerr << "wrap" << endl;
sIter = sList.begin();
}
return *sIter;
}
int main()
{
list<Test> tList;
list<Test>::iterator tIter;
Test t1;
t1.addToList("t1-s1");
t1.addToList("t1-s2");
t1.sIter = t1.sList.begin();
tList.push_back(t1);
tIter = tList.begin();
for (int j = 0; j < 3; j++)
cerr << t1.getNext() << endl;
cerr << endl;
for (int j = 0; j < 3; j++)
cerr << tIter->getNext() << endl;
}
------- snip
Prvni cyklus probehne jak ma, druhy cyklus zpusobi SIGSEGV, protoze
nezafunguje podminka v metode getNext(), iterator se zvetsi a ukazuje
buhvikam.
Neco mi uniklo nebo jde snad o chybu v kompilatoru? Pouzivam ted
gcc-2.96-52, ale delalo mi to i s egcs z RH 6.0 a 6.2. Bohuzel nemam
moznost vyzkouset to na jine platforme :(
S pozdravem
--
Michal Krause /\
ICQ: 7665279 Informace (nejenom) ze sveta Linuxu /\/ \
email: mike na navrcholu.cz ______ http://www.root.cz/ ______ NAVRCHOLU.cz
All rights reversed
Další informace o konferenci Linux