Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: cecilio on January 15, 2013, 07:39:20 pm

Title: Where is the problem? My code? GCC? CodeBlocks?
Post by: cecilio on January 15, 2013, 07:39:20 pm
I am debugging some code. Execution has arrived to this method:
Code
void EventNotifier::notify_observers(SpEventInfo pEvent, Observable* target)
{
    std::list<Observer*>::iterator it;
    for (it = m_observers.begin(); it != m_observers.end(); ++it)
    {
        Observable* observedTarget = (*it)->target();
        bool fNotify = (observedTarget == target);
        ...
The last sentence of previous excerpt has been executed. Both variables, observedTarget and target, have the same value but boolean fNotify is false! (please see attached image)

I have no idea of were is the problem. Any help is greately appreciated. Thank you

Cecilio Salmeron
www.lenmus.org

[attachment deleted by admin]
Title: Re: Where is the problem? My code? GCC? CodeBlocks?
Post by: p2rkw on January 15, 2013, 07:58:42 pm
Are you using multiple inheritance?
Title: Re: Where is the problem? My code? GCC? CodeBlocks?
Post by: oBFusCATed on January 15, 2013, 08:14:39 pm
I have no idea of were is the problem.
You've asked the question on the wrong forum... see the details in the rules http://forums.codeblocks.org/index.php/topic,9996.0.html
Title: Re: Where is the problem? My code? GCC? CodeBlocks?
Post by: cecilio on January 15, 2013, 08:24:14 pm
To p2rkw:

Thank you that's a great clue. I do use inheritance. But both pointers are casted to Observable. How should I do it?


To oBFusCATed
Sorry! When I posted the question I thought it could be something related to Code::Blocks. And the description of the forum is "General (but related to Code::Blocks)
If your post doesn’t fit any of the other topics, post it here :). This is NOT a general programming board.". Sorry again. Please, remove my post if not appropriate. Thank you.



Title: Re: Where is the problem? My code? GCC? CodeBlocks?
Post by: herrtool on January 17, 2013, 03:20:53 am
You're comparing addresses, not values.  You mentioned the values are the same but you didn't mention the addresses.  This is a fairly common mistake (I've made it several times).  :)  Dereference the pointers (assuming Observable has an overloaded == operator).

P.S. - Yes, wrong forum but damage is done.  Hopefully this answer solves your problem (or better yet, you've figured it out already).
Title: Re: Where is the problem? My code? GCC? CodeBlocks?
Post by: cecilio on January 17, 2013, 05:39:05 pm
Thank you for answering. I'm still fighting with this issue.

> P.S. - Yes, wrong forum but damage is done.

Sorry again. But I would appreciate a more positive feedback: where should i have posted this question?
Announcements? Not an announcement
Using Code::Blocks? Not a question about the usage of Code::Blocks
Embedded development? Nop
Nightly builds? Nop

So IMHO only two options:

Help [Code::Blocks installation/troubleshooting issues]
General (but related to Code::Blocks)

I thought that Help forum was perhaps more oriented to CB installation issues so I decided (wrongly, I see) to post to General forum. I thought that my problem could be some issue with Code::Blocks debugger.

Really, Which forum should I have used?

Never mind. Sorry for all the damage. Please delete this topic. Thank you.
Title: Re: Where is the problem? My code? GCC? CodeBlocks?
Post by: herrtool on January 17, 2013, 05:48:02 pm
I didn't mean to offend you.  I don't mind that you posted the question.  What I meant was that I was hoping that admins don't mind that I give you a pointer on your problem.

Code
void EventNotifier::notify_observers(SpEventInfo pEvent, Observable* target)
{
    std::list<Observer*>::iterator it;
    for (it = m_observers.begin(); it != m_observers.end(); ++it)
    {
        Observable* observedTarget = (*it)->target();
        bool fNotify = (observedTarget == target);
        ...

replace

Code
bool fNotify = (observedTarget == target);

with

Code
bool fNotify = (*observedTarget == *target);

If you get a compiler error then you need to define a function that's visible to your function above.

Code
bool operator==(const Observable& left, const Observable& right)
{
  return left.foo_member_data_stuff == right.foo_member_data_stuff;
}
Title: Re: Where is the problem? My code? GCC? CodeBlocks?
Post by: p2rkw on January 17, 2013, 05:55:04 pm
@OP: Why you cast these pointers in watch window to lomse::lmoLink* ? Cast them to void* to see their real value. And don't use c style cast while using multiple inheritance. Use static_cast and dynamic_cast instead.
Title: Re: Where is the problem? My code? GCC? CodeBlocks?
Post by: MortenMacFly on January 17, 2013, 06:52:06 pm
Topic locked as it violates the forum rules. Nevertheless, enough hints are given anyways...