Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Classes as arguments

Posted on 2004-10-24
4
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

610 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