I am new here, but browsed around looking for a solution to my problem to no avail. I have a C++ program with two objects (A and B). In my main, I create both of the objects, and even run a function in A. However, I want object B to be able to use object A. I try passing object A into an initialization function so that every function in object B can use object A, but I keep getting segmentation faults (Program received signal EXC_BAD_ACCESS, Could not access memory).
Is this possible? If so, what changes need to be made so I can call runSomething() on object A inside of object B? Below is my (simplified) code snippet:
#include <stdio.h>
class A{
public:
void runSomething();
};
void A::runSomething(){
printf("In A runSomething\n");
}
class B{
public:
void init(A * AnewObject);
A * AObject;
};
void B::init(A * AnewObject){
AObject = AnewObject;
AObject->runSomething();
}
int main(int argc, char *argv[]){
A * mainA;
mainA->runSomething();
B * mainB;
mainB->init(mainA);
}
I am expecting the runSomething to be called twice on object A, once in the main, and the second time through mainB.
I realize this is a simple question, that has probably been answered before. If there is a keyword I should be looking up, please let me know. I tried nested classes, but that didn't seem to be this issue.
Your simplified program runs without any errors on my computer, and the code looks perfectly sane to me.
Although wait a minute.
@Line 20: you may wish to switch the operands; the way it is, AnewObject is being assigned the value of your AObject. Did you mean to change the value of AObject with that line?
Also, change line 21, depending on your intention to runSomething() from the argument which will be deleted on completion of that function or AObject.
Thanks for the reply. If it helps, I am running this on a Mac using g++ 4.2.1. What I meant to do is have AnewObject be a reference to AObject.
In the grand scheme of things for the program I am currently writing, AObject is a GUI. B is just another object. However, B does things and contains variables that I may need to place in the same GUI. So, it isn't that I want to create a new instance of AObject, instead I want to manipulate *and call functions contained in* AObject inside of BObject.
You were correct about my initial code @line20. I meant to change the value of AObject in that line. I have updated the code snippet above. However, I am still getting the same segmentation fault.
My computer has almost identical specifications as yours. Admittedly, I was playing around with some system files, but... nothing regarding memory allocation, deallocation, and use in general.
Interesting. I'm sure there's something obvious that's right in front of my nose that I'm missing.
I'm sure there's something obvious that's right in front of my nose that I'm missing.
Indeed :P
@Linkshot:
You never instantiate a B object. This is a problem, because in B::init you try to set a member variable of an object that doesn't exist. That and the fact that your mainB pointer could point anywhere are enough to get you a nice seg-fault :D
Aha! Thank you so much. I was able to resolve this issue in my simple example using your feedback. By simple adding A* mainA = new A and B* mainB = new B, the program ran as I expected. Now to see if these changes will work in my much larger program.
Oh now that's just brilliant of me. Something that simple, eh? Funny thing, I never make that mistake while programming but show me a piece of code with it and I'll skip right over it. Tsk tsk. Silly me.