0

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++;
}
}
6
  • 2
    Try it->artikelNr. (*it isn't a pointer.) Commented May 7, 2015 at 20:03
  • @molbdnilo What if Artikel is a pointer !? Commented May 7, 2015 at 20:08
  • @DieterLücking "base operand of '->' has non-pointer type 'Artikel'" kind of suggests that it isn't. Commented May 7, 2015 at 20:13
  • @molbdnilo I tried it->artikelNr and I no longer have the error about the type, instead though, I got two of these: error: invalid use of member function (did you forget the '()' ?)| Commented May 7, 2015 at 20:21
  • @EmilCoder If artikelNr is a function, you need the "()", but you didn't write any. Commented May 7, 2015 at 20:25

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

12 Comments

I tried it and it removed the previous error about the different types but now I recieved another error instead: error: invalid use of member function (did you forget the '()' ?) Any idea what that means? Thanks for taking your time btw!
@EmilCoder More code is needed to check this. Put paranthesis if artikelNr is a function. If not, the error belongs to another portion of the code.
@EmilCoder I suggest you to use a proper IDE (like Eclipse) to write your code. That makes it easier to debug your code. A good one would show that you're missing the paranthesis while you're writing.
Ok, I can send all the files through drive if thats ok. artikelNr is an int variable btw. drive.google.com/…
Ok, I have only used Code blocks and Microsoft visual c++ studio so far but will definitely try that one out!
|
0

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++;
}
}

5 Comments

I tried the '.' at first, but it did not work and I got the error: 'std::list<Artikel>::iterator' has no member named 'artikelNr'|
it is 'std::list<Artikel>::iterator' but (*it) is Artikel
Yes, the iterator is of the type Artikel and the data member I am trying to access is of the type int. Sorry, I dont really get it?
the iterator is a pointer to some data of type Artikel, you can access to the referenced data in two ways when coding in c++. it->member, (*it).member. When you write 'it.member' you'll get an error, it is a pointer not a class/struct. I hope it helps
As I saw in other answer: to access a function you must use '()' after the function name funkArtikel.artikelNr() < it->artikelNr(), funkArtikel.artikelNr() < (*it).artikelNr() might work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.