EmilCoder
EmilCoder

Reputation: 3

How to access data member in a list, with different type than my list

I am writing a program with a list of the type Artikel and in the list there are several elements that each contains 4 data members, 2 int and 2 string. The list is sorted by int artikelNr and the function below is supposed to add a new Artikel to the list at the right spot with its artikelNr in mind. Although since the list is of the type Artikel and the data member I am comparing with is Int I get the error: base operand of '->' has non-pointer type 'Artikel'|

I have tried to make an iterator *it to the list of the type int but it does not work since the list is of the type Artikel.

void Lager::lagg_till_registret(Artikel funkArtikel)  
{
bool check = false;
list<Artikel>::iterator it = listaMedArtiklar.begin();
while(check == false){
   if(funkArtikel.artikelNr < (*it)->artikelNr){
        listaMedArtiklar.insert (it,funkArtikel);
        check = true;
    }
    else
        it++;
}
}

Upvotes: 0

Views: 57

Answers (2)

omerfarukdogan
omerfarukdogan

Reputation: 869

Best way to solve it is this:

void Lager::lagg_till_registret(Artikel funkArtikel)  
{
bool check = false;
list<Artikel>::iterator it = listaMedArtiklar.begin();
while(check == false){
    if(funkArtikel.artikelNr < it->artikelNr){
        listaMedArtiklar.insert (it,funkArtikel);
        check = true;
    }
    else
        it++;
   }
}

Also you can do this (this is equal to the first one, but looks ugly)

if(funkArtikel.artikelNR < (*it).artikelNr)

Upvotes: 1

danpeis
danpeis

Reputation: 166

I don't recall it well but if yout dereference a variable in c++ you don't need the '->' operator. Try to use '.' instead:

void Lager::lagg_till_registret(Artikel funkArtikel)  
{
bool check = false;
list<Artikel>::iterator it = listaMedArtiklar.begin();
while(check == false){
   if(funkArtikel.artikelNr < (*it).artikelNr){
        listaMedArtiklar.insert (it,funkArtikel);
        check = true;
    }
    else
        it++;
}
}

Upvotes: 0

Related Questions