?
Solved

Classes as arguments

Posted on 2004-10-24
4
Medium Priority
?
229 Views
Last Modified: 2010-04-01
When passing a class as a parameter to another class' method, is there any thing I should bear in mind?   Maybe some overloading of the = operator, or something else?

I am getting a "Debug Assertion Failed" when the method is called.   I commented out the entire inside of the method, so the code now looks something like this:

======CODE=========
class A{
public:
    contructor();
    destructor();
    someMethods();
private:
    int someData;
    char someMoreData;
};

class B{
public:
    constructor();
    destructor();
    void brokenMethod(A instanceOfA);
private:
    int yetMoreData;
}

void B::brokenMethod(A instanceOfA)
{
    // ALL COMMENTED OUT
    cout << "Returning..." << endl;
}

int main(int argc, char** argv)
{
    A A;
    B B;
    cout << "Calling brokenMethod." << endl;
    B.brokenMethod(A);
    cout << "Returned."

    return 0;
}

======OUTPUT=========
The output looks like this:
Calling brokenMethod.
Returning...

======ERROR=========
Everything complies fine.   During runtime I get:
Debug Assertion Failed!
Program: <Path to my program>
File: dbgheap.c
Line: 1011
Expression: _CrtIsValidHeapPointer(pUserData)

As always, any help is super duper appreciated.

pdm
0
Comment
Question by:Diceman_01
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12395479
>>When passing a class as a parameter to another class' method, is there any thing I should bear in mind?

You should prefer passing by reference instead of passing by value.
Example:

void brokenMethod(A &instanceOfA);//In your class header


void B::brokenMethod(A &instanceOfA)
{
   // ALL COMMENTED OUT
   cout << "Returning..." << endl;
}
0
 
LVL 30

Accepted Solution

by:
Axter earned 150 total points
ID: 12395486
I notice that your A class has data member someMoreData as a char type.

Are you trying to use this data member as a string?
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 100 total points
ID: 12395542
The above code run fine for me. Which compiler do u use. also, are u doing something more than u have shown above.

Amit
0
 

Author Comment

by:Diceman_01
ID: 12395881
I am a dumbass.   The problem was in the destructor code for the class being used as the parameter.  

I wouldn't have seen it without asking this question.

Thanks for the input.   Points are split.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question