void fishing(vector<Fish*> &basket);
int printBasket(vector<Fish*> &basket);
int main()
{
vector<Fish*> FishVec;


Fish* aFish;

int totalweight=0;
int i=0;
while (totalweight<15000)
{
fishing(FishVec);

aFish=FishVec.at(i);

aFish->printMe();

if (aFish->acceptable())
{
totalweight+=aFish->getWeight();
cout<<"total weight:"<<totalweight<<endl;
cout<<" Has been put in the basket"<<endl;



}
else
{

cout<<" Was released"<<endl;
}
i++;
cout<<endl;
}
}
void fishing(vector<Fish*> &basket)
{
int i=RandomInt(1,4);
Fish* aFish;

if (i==1)
{
cout<<"caought:";
aFish=new AustralianBass();
basket.push_back(aFish);

}
else if(i==2)
{
cout<<"caought:";
aFish=new ShortFinedEel();
basket.push_back(aFish);
}
else if (i==3)
{
cout<<"caought:";
aFish=new EelTailedCatfish();
basket.push_back(aFish);
}
else if (i==4)
{
cout<<"caought:";
aFish=new GippslandPerch();
basket.push_back(aFish);
}

}
int printBasket(vector<Fish*> &basket) //suposed to be print content of the basket but it     is not working what is wrong with this .
{

Fish* aFish;
for(int i=0;i<basket.size();i++)
{
aFish=(basket.at(i));
aFish->printMe();
}
return 0;
}

Dani AI

Generated

Short diagnosis and practical next steps (building on and ).

The print routine you posted that walks the vector and calls the per‑fish print method usually works. The common reasons you would not see the derived‑class output are: (1) the basket‑printing function is never invoked where you expect it, (2) the base class print method is not declared for runtime polymorphism so calls through a base pointer call the base implementation, or (3) an out_of_range exception or indexing bug prevents the loop from running. is correct to point at polymorphism; 's reminder about indentation is useful because bad scope/flow can hide logic errors.

Concrete checks and fixes:

  • Make sure the print function gets called after the vector is populated (or call it explicitly to test). Add a quick check that the container size is what you expect before indexing. See vector::at documentation for behavior on bad indexes: std::vector::at.
  • Use runtime polymorphism correctly and give the base class a virtual print method so derived overrides run. Also provide a virtual destructor to avoid undefined behavior when deleting through a base pointer; see the language notes on virtual functions: C++ virtual functions.
  • Avoid manual new/delete for ownership. Prefer RAII: store smart pointers or values in the container to prevent leaks and make lifetime explicit. See std::unique_ptr for a simple owning pointer: std::unique_ptr.

Quick debugging tips: add logging for vector size, wrap calls to at() in a try/catch to surface exceptions, compile with warnings enabled (e.g., -Wall -Wextra), and run a memory checker if objects are allocated dynamically.

Recommended Answers

All 2 Replies

Your printbasket function seems to be fine.

This is not a solution but it would make it easier to read if you used indention to show scope.

The problem does not appear to be in printbasket.

Look at fish::printme. Are the different fish classes derived from the fish class? Calling printMe() from a Fish pointer will call the fish handler if no inhertitance is used. You obviously want it to call fishtype::printme()

This is what you need, if you don't already know about virtual methods and inheritance you should read about it.


class Fish
{
public:
virtual void PrintMe()=0;
};

class FishType : public Fish
{
virtual void PrintMe();
};

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.