I am basically trying to get the vector to store information entered by the user and then display it. Having trouble displaying information. Here is the code that i had written
class Vehicle
{
private:
string vehColor;
int regNo;
public:
Vehicle(string color, int reg):vehColor(color), regNo(reg){}
~Vehicle(){}
virtual void display()
{
cout << "Vehicle's color is:" << vehColor << endl;
cout << "Vehicle's Registration number is:" << regNo << endl;
}
};
class Car : public Vehicle
{
private:
string fuelType;
int engineCapacity;
public:
Car(string vehicleColor, int reg, string fuelT, int capacity): Vehicle(vehicleColor, reg), fuelType(fuelT), engineCapacity(capacity) {}
Hello,
wrap your code in [.code][./code] tags next time. Without dots, of course :)
Here's reference to for_each: http://www.cplusplus.com/reference/algorithm/for_each/
Basically, you're using it wrong. However, since I'm not familiar to this function, I do not know how to use it properly to use member function.
Instead, I'd recommend this loop type:
// range for
for ( auto v : storeInfo )
{
v->display();
}
// or lambda
for_each( storeInfo.begin(), storeInfo.end(), [](Vehicle * v){ v->display(); } );
generic - I would highly recommend you to upgrade MinGW then. I suppose you're using windows. It doesn't really matter what IDE you use, or what version of your IDE you use(in other words, it doesn't really matter if you use old or new C::B). The thing that you should "replace" is compiler. And you can do that by downloading new one and installing it. It's good practice for you, since you'll eventually need to do that anyway, and the faster you learn it, the more you'll know. And it's useful knowledge to have :)
Back to the topic - Firstly, I'd recommend using this syntax:
for(std::vector<Vehicle*>::iterator iter = storeInfo.begin(); //rest...)
Or even this one(it's easier to use):
for(auto it = storeInfo.begin(); //rest...)
I believe the variable there is limited to the scope of the loop, so memory it occupies(iterator) is automatically freed after loop.
And yes. Your function, if you don't have the loop type I provided you with, would look like this:
1 2 3 4
for(auto it = storeInfo.begin(); it != storeInfo.end(); ++iter)
{
it->display();
}
It's just the plain old for loop, just using vector's iterator :)
Cheers!