FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


c++ and auto_ptrs





snowboardalliance
Ok, I'm really stuck here.
Basically, I'm making a game and I have a class Actor with a lot of class members, so I used a Factory that reads an XML file.
Anyway, I decided to try to let the Factory return auto_ptr<Actor>(new Actor(lots of params));

It seemed to work in test code. Then I had a method that takes a "const Actor &a".

That was the problem, no matter how I pass my auto_ptr a (i.e. *a, *(a.get())) my debugger is showing that all the values in Actor are garbage.

How can I pass an auto_ptr to a function that expects a plain const reference? Where should I start to fix this?

Thanks in advance, let me know if example code is needed.
snowboardalliance
Nevermind, I feel really dumb. After hours of debugging, I found the error was in different code. My debugger won't read the auto_ptr's correctly so I assumed they were the cause.

The actual problem was that my data (size_t) was being used in operations with parameters (int). I guess that converted the int to size_t and some of my comparisons would mysteriously fail (because they were becoming unsigned ints and could never be less than 0).

I wish I had known that this happened:
Code:

size_t s = 40;
int i = 10;

if (i - s < 0)
{
cout << "this will never happen" << endl;
}

if ((int)(i - s) < 0)
{
cout << "this will always happen" << endl;
}


Frustrating...
AftershockVibe
-Wall is your friend.
Your compiler will shout at you if you do something suspect like that. There's really no excuse for not using this (or whatever your compiler has instead to show all warnings) - you can just ignore it if you know better. Generally, it's easier to fix it and make your code more robust though.
Cool
snowboardalliance
AftershockVibe wrote:
-Wall is your friend.
Your compiler will shout at you if you do something suspect like that. There's really no excuse for not using this (or whatever your compiler has instead to show all warnings) - you can just ignore it if you know better. Generally, it's easier to fix it and make your code more robust though.
Cool


I did have -Wall enabled in codeblocks, but I never saw any errors about it. I got a few errors about converting a double to an int I think, but nothing warned me about unsigned ints. Oh well, I know now.
Related topics
Command and Conquer
Funny C variable
What can I do with C-Panel?
HELP ME in C++
Languages in c++
C++ TUTORIAL : INTRODUCTION TO C++
I searching for a site
Image Processing in C++ by Shirish
C en C++
C# tutorials
C en C++
C#,Visual Basic.NET,Visual Studio.NET Videos
[Question] Visual Basic C++, PHP, mySQL language guides...
et un appart sur Paris pour septembre, c'est possible ?
My site : www.sunsoft.c-o.in
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.