0

I'm currently writing some code that iterates through a vector and calls a simple print method for every element within that vector; however, I have very limited experience with vectors and I'm having some issues getting the compiler to accept this one particular segment of code:

std::vector<Buyer *>:: iterator it;
    Buyer *b;
    for(it = buyers->begin(); it != buyers->end(); ++it)    {
            b = *it;
            cout << b->getName();
    }

where buyers is a vector containing Buyer pointers. I'd much rather use an array for this purpose, but as I'm required to use a vector for this particular point, I'm unsure of how to iterate through it and ended up trying an iterator to get through.

The error message that springs up is:

Retailer.cpp:37:17: error: base operand of ‘->’ is not a pointer
for(it = buyers->begin(); it != buyers->end(); ++it) {
             ^
Retailer.cpp:37:40: error: base operand of ‘->’ is not a pointer
for(it = buyers->begin(); it != buyers->end(); ++it) {

From what I understand, there's apparently a pointer somewhere in that mess I'm not dereferencing, but I'm not sure which element that is or where I should dereference it... any ideas for a programmer who's had little experience with c++ vectors/iterators?

4
  • What is the declaration for buyers? Commented Apr 29, 2014 at 23:17
  • buyers is actually inherited in this class, but it's basically declared as vector<Buyer *> buyers Commented Apr 29, 2014 at 23:22
  • Basically declared as a vector? Can you be more specific? Your problem is most likely due to the specifics of the declaration. Commented Apr 30, 2014 at 0:54
  • Well, actually that's exactly how it's declared(followed by ; of course), but I believe I've fixed the problem now. Oddly enough, I seem to have solved it by simply declaring class Buyer; at the beginning of the Seller.h file. It may have to do with the fact that both are descended from the same class(which I neglected to mention), or the error simply moved to a different location in the error list - given there are still a dozen more errors in the entire project. Commented Apr 30, 2014 at 1:08

2 Answers 2

3

Your error is that byuers is not a pointer, but you write buyers->begin(). This should have been a buyers.begin() instead. (The same for buyers->end() of course).

You can simplify your code significantly, if you use modern c++11:

for(Buyer* b : buyers)
{
     // do something
}
Sign up to request clarification or add additional context in comments.

4 Comments

Or even more modernly, for(auto b : buyers).
@MooseBoys That is how I would usually do it, but I think this makes it a bit easier to understand for someone unused to c++11
hmm, while I tried that, but then the error message I get reads "request for member ‘begin’ in ‘((Retailer*)this)->Retailer::<anonymous>.Seller::buyers’, which is of non-class type ‘int’ ".
@Vincents In that case we need more context - I hardly think that you would mistake an int for an vector<Buyer*>...
0

The other answers have already explained what your error is but they have not given you an answer in C++ 03, which you may be using (since I'm not sure, I'll add my own answer).

In the old C++, C++ 03, you would need to change:

for(it = buyers->begin(); it != buyers->end(); ++it)    {

to

for(it = buyers.begin(); it != buyers.end(); ++it)    {

As was already stated in the other answers, this is because the -> requires a pointer to the left of it but you don't have a pointer, you have an object so you need to use the . operator.

1 Comment

Well actually I'm using c++11, but I agree the issue lies in the use of the -> operator... unfortunately switching to . doesn't really help matters, it just gives a different error message. I believe I'm losing track of what data types I'm using, so I'm going to try experimenting around with different operators until I find the right code. Thanks for the consideration, though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.