Vector C++

Create a vector in the main program that will store the dynamic objects of the Clothing class and create the function getClothesTotal (vector <Clothes *> shopping), which calculates the value of the entire purchase.
I have this code in main, and i tried to create function getClothesTotal, but it doesn't works. If someone knows how to solve this please help :D
1
2
3
4
5
6
7
8
9
10
vector<Clothes> MyClothes;
    Clothes* a = new Clothes("man ", "coat",42,150);
    Clothes* b = new Clothes("woman", "coat",38,160);
    Clothes* c = new Clothes("woman","dress",36,80);
    a->print();
    b->print();
    c->print();
    delete a;
    delete b;
    delete c;
Why doesn't it work?

Do you get compile errors?

Runtime errors?

If so what exactly do the errors report?

Please show a complete program that illustrates your problems.

In your snippet I see a few issues.

First you never use the vector.

Second why all the pointers? Why not just use a non-pointer instance of your class since there doesn't seem to be any inheritance in the class?

Jim
I have this function in my class:
double getClothingTotal(std::vector <Clothing *> shopping) {
double total = 0.0;
std::vector<Clothing*>::iterator VectorPointer;
for (VectorPointer = shopping.begin(); VectorPointer != shopping.end(); ++VectorPointer) {
total += VectorPointer->getPrice();
}
return total;

but there are so many errors. First i need to store dynamic objects in vector, and then to create this function, but it doesn't works.. I don't even know where to start
but there are so many errors.


So post the complete program, along with the complete error messages, all of them, exactly as they appear in your development environment.

Also have you seen ranged based loops, they are meant to simplify that type of loop,
1
2
3
4
5
for(auto& itr : shopping)
{
     total += itr.getPrice()  
}
Last edited on
double Clothing::getClothingTotal(const vector<Clothing*> &shopping) {
return 0;
}
I create function and now i have to use getPrice() inside this function to calculate all the prices
Topic archived. No new replies allowed.