Hi all,
I've had a bit of frustration with making a class and accessing the private variables from the functions. So here's the whole scoop (code snips are located below). I've created my own custom Physics class to emulate physics, and in it I am using a vector to keep track of my ball objects I will create. The problem is that even if I completely initialize 4 balls in the constructor (which works fine by the way) as soon as I try to change their properties or call push_back to make a new object, during run it gives me Access Violations. I know this is due to a bad pointer, but that is why I switched to a vector, because it was supposed to do all the dirty work for me. My code is below, please help!
The things to notice are how a call to the private variable in the "updateBalls()" method seems to work fine. But no matter what I do I can't even make the same call from "createBall()" This is completely baffling me.
Thanks ahead of time! Physics.h File: -
#include <vector>
-
#include "3DBalls.h"
-
#include "Ball.h"
-
-
-
class Physics
-
{
-
private:
-
//Ball myBalls[MAXNUMOFBALLS];
-
//int numberOfBalls;
-
std::vector<Ball> myBalls;
-
-
public:
-
Physics();
-
void createBall(D3DVECTOR position, float mass, float radius);
-
int getNumOfBalls();
-
void collisionDetection();
-
void collisionPrediction();
-
void addGravity();
-
void updateBalls(float timeDelta);
-
bool insideBall(float posX, float posY, float posZ, float radius);
-
void physicsLoop(float timeDelta);
-
D3DVECTOR getBallPosition(int ballNum);
-
void release();
-
};
-
Physics.cpp File -
#include "Physics.h"
-
-
-
Physics::Physics()
-
{
-
//Need to create struct to handle list of objects
-
int i;
-
D3DVECTOR temp = {0.0f, 100.0f, 0.0f};
-
for(i=0; i<MAXNUMOFBALLS; i++)
-
{
-
// Fully initialized Balls from constructor
-
myBalls.push_back(*(new Ball(temp, 100.0f, 5.0f)));
-
temp.z +=30;
-
}
-
}
-
-
void Physics::createBall(D3DVECTOR position, float mass, float radius)
-
{
-
// If this is commented the program runs fine
-
myBalls.push_back(*(new Ball(position, mass, radius)));
-
// Can't even do this!!!
-
myBalls.at(0).getRadius();
-
}
-
-
-
void Physics::updateBalls(float timeDelta)
-
{
-
int i;
-
for(i=0; i<myBalls.size(); i++)
-
{
-
// For some reason this works fine! It doesn't make sense
-
myBalls.at(i).calculateNewPosition(timeDelta);
-
}
-
}
7 2006
Instead of doing two or three operations on a Ball object in one line of code why dont you try breaking it down even more. Then you can see what calls are causing problems. For example - Ball tempBall = myBall.at(i);
-
tempBall.calculateNewPosition(timeDelta);
-
//use debug to verify that the new position was calculated properly
-
myBall.at(i) = tempBall;
Now you can watch every line of code as it executes and see what the result was.
Instead of doing two or three operations on a Ball object in one line of code why dont you try breaking it down even more. Then you can see what calls are causing problems. For example - Ball tempBall = myBall.at(i);
-
tempBall.calculateNewPosition(timeDelta);
-
//use debug to verify that the new position was calculated properly
-
myBall.at(i) = tempBall;
Now you can watch every line of code as it executes and see what the result was.
Thanks for a quick reply. I guess I should have specified a bit further. I've actually debugged this code line by line and the Access Violation actually occurs inside of the vector class. So somehow the vector pointer is getting corrupt. (possibly a compiler bug?) I'm just looking for some sort of confirmation that I'm not doing something entirely stupid.
The same thing happened when I used my own array of Balls. For some reason when I call createBall() I get access violations no matter which variable I am trying to access. I can created an Int in the class, and for some reason I get Access violations from the createBall(); class, even if I am only trying to read it in memory.
And actually, as long as the Balls are created in the constructor (ie I use a loop to make 5 Balls), all the other code runs fine EXCEPT createBall(). The problem is I would like to make new balls on command.
Thanks for a quick reply. I guess I should have specified a bit further. I've actually debugged this code line by line and the Access Violation actually occurs inside of the vector class. So somehow the vector pointer is getting corrupt. (possibly a compiler bug?) I'm just looking for some sort of confirmation that I'm not doing something entirely stupid.
The same thing happened when I used my own array of Balls. For some reason when I call createBall() I get access violations no matter which variable I am trying to access. I can created an Int in the class, and for some reason I get Access violations from the createBall(); class, even if I am only trying to read it in memory.
And actually, as long as the Balls are created in the constructor (ie I use a loop to make 5 Balls), all the other code runs fine EXCEPT createBall(). The problem is I would like to make new balls on command.
Try storing pointers in the vector: -
Ball *pball = new Ball(position, mass, radius);
-
myBalls.push_back(pball);
-
That might be difficult if your design depends on objects.
Declare the vector like this: -
std::vector<Ball *> myBalls;
-
Also, don't forget to free your memory. If you use objects you have to do this: -
Ball *pBall = &myBalls[i]
-
delete pBall;
-
EDIT: Reading over your post, maybe it's not a good idea because it works fine in the constructor.
Can you show us the copy constructor for the Ball class?
kind regards,
Jos
Try storing pointers in the vector:
Code: ( cpp )
Ball *pball = new Ball(position, mass, radius);
myBalls.push_back(pball);
This just ducks the real issue: The Ball class has no proper copy constructor.
It has to be this since vector does move things around when some methods execute.
By having a class of pointers, at least the pointers will copy and the error should go away only to resurface elsewhere when a copy constructor is needed.
Lastly, a vector of pointer is is not a good idea since you can't tell when it's safe to delete the pointer. Certainly, the object can't be deleted if there are other pointers to it in the program. You uses handles in a case like this. That is, you need a vector of handles to Ball. There is an article in the C/C++ Articles forum that shows you how to do this.
Can you show us the copy constructor for the Ball class?
kind regards,
Jos
This could be my problem. Being natively used to java, this is my first real C++ program. I've never even heard of a copy constructor requirement. What exactly needs to be in a copy constructor? Is this simply to create a pointer to a new object create with the same attributes? If this is the problem that would be great!
Yeah, so I feel like a complete idiot. Horrah for being a noob! Consider this thread closed.
Like the idiot I am I ran a call to "createBall" before I made an instance to the class. I found this out by putting a break point in the constructor and the createBall function. And I was confused when it didn't stop at the break point in the constructor, and when it finally clicked I felt like crawling into a hole.
I thank everyone for their help and I found out I didn't need a copy cconstructor since I don't have pointers inside the ball class, it would simply create its own satisfactory copy constructor.
Thanks Again!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Robert Neville |
last post by:
I am having some trouble with some old code revolving around custom
form navigation buttons. My main form has a sub-form with these custom
navigation buttons. In other words, the code should be...
|
by: Rolf Welskes |
last post by:
Hello,
I have an ObjectDataSource which has as business-object a simple array of
strings. No problem.
I have an own (custom) control to which I give the DataSourceId and in the
custom-control...
|
by: Charles Zhang |
last post by:
I created a custom server control whose rendering logic relies on
session variables and cookies. It works just fine at run time. The
problem is at the design time, because session variables and...
|
by: Nick Gilbert |
last post by:
Hi,
How can I create a custom control which will wrap its content in a
header and footer?
eg:
Is it possible to create a .NET user control which can surround other
controls? eg:...
|
by: forest demon |
last post by:
i have a custom control that gets loaded at runtime. i need to be
able to access a property of a control thats part of the main form,
through the clcik event of the custom control.
i may be...
|
by: Tomasz J |
last post by:
Hello Developers,
I have a control derived from System.Web.UI.WebControls.WebControl. Control
has this property:
public string Value
{
set { _value = value; }
get { return _value; }
|
by: Linda Liu[MSFT] |
last post by:
Hi George,
I have downloaded your sample solution and built it on my machine. I got a
compilation error indicating that the type of "CustomResources" doesn't
exist in the following xaml code:
...
|
by: gerry |
last post by:
I am trying to create a custom container control that will only ever contain
a specific type of control.
At design time, when a control of a different type is added to the container
I would like...
|
by: =?Utf-8?B?UmljaEI=?= |
last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I
would like to dynamically add panes to the control with a form template added
when the pane is added. I have tried...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |