Click here to Skip to main content
14,979,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a program in c++ using eclipse. I have a vector which needs to store pointer objects. When I compile the program, it crashes saying the program has stopped working. How can I fix this error?

C++
//declaring pointer to TopStock class
TopStock* stock = new TopStock(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]);
//adding the pointer to v_stockRows
v_stockRows.push_back(stock);

//deleting pointer
//delete stock;



Here the row is another vector (values are assigned)

What I have tried:

I tried this
v_stockRows.push_back(&stock);
Posted
Updated 19-Jan-20 23:16pm
Comments
CPallini 20-Jan-20 8:56am
   
When you RUN the program it crashes. You should post more code, in order to make us able to fix it.
Rick York 20-Jan-20 11:16am
   
What we really need to see is how v_stockRows was declared. The act of adding data to the vector crashes the program so that means in the push_back function is causing the problem. The key to that is in how the data is stored in the vector so we need to see that to determine what the problem is. My guess is there is a problem with a copy constructor.

We can't tell - we don't have access to your data, or to your code while it's running - and that what you need to work out why. Heck, we don't even know what the error message is, and that's normally very, very relevant!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Quote:
When I compile the program, it crashes saying the program has stopped working.

Posting 2 lines of code that refer to mystery other code is no help to understand what you did wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900