Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing reference to class as a function parameter

Status
Not open for further replies.

hedvule

Programmer
Oct 25, 2004
7
CH
Hi,

i have following problem: I have a function in which I need to change some variables of class. I tried to pass a reference to this class as a function parameter but I get a violation acces warning and the function doesn't run. Could you please help me?

The code looks like this:

Class1 myClass1;

void DoSomething(int n, Class1 &r_myClass) {
r_myClass.ChangeVar(n);
... // some other tasks
}

I can't define this function as a member function of the Class1 for some reasons.

Hope it's not too confused...
Thanks for help, H.
 
Make sure you #include the Class1.h file before you try to define this. When you have compiler errors or warnings, don't just say what they are, actually copy and post what the compiler is telling you. It's much more informative.
 
Hi,

the class1.h file is included. My compiler sais everything is fine, the problem is following:
when I run the program, the function doesn't run when called, but the program skips it and finish correctly. When I try to run it in debugger, then appears a small window saying just:
An access vilolation (Segmentation Fault) raised in your programm.
That's it and I can't figure out where is the problem.
 
What you've provided isn't enough for me to figure this out. Could you post more code?
 
segmentation faults means your trying to access memory that doesn't belong to your program. This is typically running off the end of an array (check loop conditions for proper termination condition and check to makesure you don't have a = where there should be an ==), or your derefferencing a pointer that is pointing to NULL or Junk (that is the momey it's pointing to went out of scope, was deleted or was never created in the first place).

Beyond that you'd have to post the offending code and the error nessage.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top