I had an exam yesterday for Programming I at the end of my first semester. It was pretty basic, except I made a really, really obscure mistake that I wanted to ask the more experienced people here about:
I basically had a class Library that contained a vector that contains elements of Loan* which contains a Customer and Book class. Kind of like this:
class Library
{
vector<Loan*> loans_;
};
class Loan
{
Customer *customer_;
Book *book_;
};
I had a function which required going through all the initialized book objects and seeing which ones belonged to whom, like this:
Customer *getBorrower(Book *book)
{
vector<Loan*>::iterator iter;
for( iter = loans_.begin(); iter != loans_.end(); ++iter )
{
if( (*iter).getBook() == book )
return (*iter).getCustomer();
else
continue;
}
return 0;
}
This stumped me, because in the Netbeans 6.9 IDE the (*iter). showed me a list of all the methods of the Loan object.
I was just about to quit the exam when I loaded up VC++08 and it asked me if I didn't want to use ->
instead.
(*iter)->getBook();
worked to my chagrin.
So my question is: why did the IDE allow me to use .
and even spit out a list of the methods as well as for ->
? To my knowledge, it doesn't do this kind of madness anytime else. What exactly happened here? I'm not understanding what I tried to make the method do that gave me that weird netbeans error "Is not of type Loan*"
Second question: This is a style question. I commented to the professor about this, but he didn't seem too agreeable. The exam guidelines also had us create a public
vector<Book*>
and vector<Customer*>
.
We then accessed these from the main function to run our unit tests. I found this to be in extremely bad practise since a simple getter function would have provided us with what we needed without publicizing our information. Was I right to call this out as a bad design choice?
Error:
Library.cpp:14: error: request for member getCustomer' in
(&iter)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Loan**, _Container = std::vector >', which is of non-class type `Loan*'