|
Only a shot in the dark (don't have my docs at hand):
I assume vector::push_back() takes a const reference to the object type (which is auto_ptr<b> in your case). An auto_ptr contains an ownership flag and if assigning auto_ptrs always the L-value takes ownership. For this the R-value (the auto_ptr you try to insert in your vector) has to be modified, because it's ownerhip flag has to be cleared. And this is not possible because it has been passed as a const reference.
The error message could be translated to:
error C2440: 'initializing' : cannot convert from 'const std::auto_ptr<_Ty>' to 'std::auto_ptr<_Ty>' with [ _Ty=b ]
which makes the problem a bit more obvious.
(BTW: Does anybody know why the the C++ standard comitee did not made the ownership flag of an auto_ptr mutable, so it could be modified even in const objects?)
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
You're dead right - it's because the auto_ptr copy constructor takes a non-const reference. The reason it's like that (i.e. not using mutable on the ownership flag) is that containers require that their content elements can be copied without altering the element being copied. For example (and I'm quoting Herb Sutter's Exceptional C++), sort algorithms (particularly those based on partitioning & sorting, I suspect) take copies of elements of containers. This would result in the element being deleted, but it's reference still being in the container.
auto_ptr is really best used as a scoped pointer container, i.e. without transfer of ownership. It does help make things easier then, especially with helping to ensure exception safety.
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Thanks Stuart,
that surly makes sense and is, if one thinks a bit about it, rather obvious.
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
You're right.
The error message confused me. I thought the compiler cannot translate 'pb' to 'std::auto_ptr<_Ty>' or doesn't support nested templates or anything like that.
It seems I should provide a const constructor for auto_ptr by myself.
Thanks.
--------------------------------
You don't know what you've got till it's gone.
|
|
|
|
|
Be careful later !
dlhson2001@yahoo.com
|
|
|
|
|
...
void _Insert_n(iterator _Where, size_type _Count, const _Ty& _Val)
{
_Ty _Tmp = _Val;
size_type _Capacity = capacity();
...
It seems that the compiler could not find the operator=, but a copy constructor and the operator= have been present in the header <memory>:
template<class _Ty>
class auto_ptr
{
public:
typedef _Ty element_type;
explicit auto_ptr(_Ty *_Ptr = 0) _THROW0()
: _Myptr(_Ptr)
{
}
auto_ptr(auto_ptr<_Ty>& _Right) _THROW0()
: _Myptr(_Right.release())
{
}
auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()
: _Myptr(_Right._Ref.release())
{
}
...
auto_ptr<_Ty>& operator=(auto_ptr<_Ty>& _Right) _THROW0()
{
reset(_Right.release());
return (*this);
}
...
The compiler just ignored them.
-------------
You don't know what you've got till it's gone.
|
|
|
|
|
Did you read the posting of Stuart to this topic?
It is nearly impossible to use auto_ptr in STL containers (and any other kind of container), because you run into a bunch of problems
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
Noooo - not quite. If you look at the vector definition, void _Insert_n(iterator _Where, size_type _Count, const _Ty& _Val) , it takes a const reference. BUT - in auto_ptr , none of the assignment operators/coy constructors take a const reference parameter. As you cannot implicitly convert const to non-const, the compiler cannot use these operators! It is that simple really, honest!
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Futher to my other replies - if you want to use smart pointers in STL containers, I'd suggest you use the Boost shared_ptr class. Go to www.boost.org[^]. There's a download for the whole library there. There's a whole lot more than just smart pointers as well...
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
You know this code will crash and burn?
Don't ever use auto_ptr in collections. Have a look at some other pointer type such as shared_ptr from the BOOST library.
|
|
|
|
|
I have a CDialog based app, and I need to check restart/shutdown of the system. I should ask an user with confirmation, before closing app ( when presented the active threads ).
Why WM_QUERYENDSESSION in PreTranslate does not work?
Pavel Sokolov,
CEZEO software,
LanTalk Network,
http://www.cezeo.com
http://www.lantalk.net
|
|
|
|
|
I don't quite understand what you are asking, but if you want to restart or shutdown Windows, call ExitWindowEx().
_____________________
"They'd dearly make us pay
For laughing in their faces
And making it our way"
-Love My Way, Psychedelic Furs
|
|
|
|
|
I have tired to catch WM_QWERYENDSESSION in the PreTranslateMsg but this is not work. I found the right way, afx_msg OnQweryEndSession() ...
Pavel Sokolov,
CEZEO software,
LanTalk Network,
http://www.cezeo.com
http://www.lantalk.net
|
|
|
|
|
Hi,
I have to show a nicely formatted document with some text, lines, tables, possible pics and graphs from ActiveX controls in my app.
Could somebody please give me a pointer? Maybe some libraries?
Thanks
R.
|
|
|
|
|
I am reading "Introduction to MFC Programming with Visual C++" by Richard M. Jones. I am new to MFC and Win 32, and Visual Studio, but am otherwise a proficient and experienced programmer.
I am trying the second example from the book and it is not working as expected. Instead of a string as output all I get is integer (probably the memory address of the CString object).
#include <afx.h><br />
<br />
#include <iostream>
using namespace std;
<br />
int main()<br />
{<br />
CString s;<br />
CString sa("Strings ");<br />
CString sb = CString("are easy ");<br />
CString sc = "in MFC!";<br />
s = sa + sb + sc + '!';<br />
cout << s;<br />
<br />
::Sleep(2000);
return 0;<br />
}
As specified in the book I created a Win 32 Console App then specified "Use MFC in a shared DLL" and then built it in debug mode. I tried this with both 6.0 and .NET. The book is suposed to cover 6.0.
What is going wrong?
PS I changed the code to
cout << s.GetString();
Which works, is the book simply mistaken, out of date (it claims to cover 6.0) or am I configuring the project wrong in some way?
|
|
|
|
|
Maybe cout does not recognize a CString object.
-----
CString szText = "Testing 1 2 3";
cout << static_cast<pctstr>(szText);
-----
Kuphryn
|
|
|
|
|
You've got it in one - you need to cast it to a const char *, or get the underlying char * using a method as you have done. Or you could write an iostream inserter for CString. I have articles on CP to show you how.
Christian
We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum )
Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
|
|
|
|
|
Good day all, new day, new help me please
in this function to return the users system directory
CString CSetupApp::GetSystemDir()
{
CString szSystemDir((char)0, MAX_PATH);
::GetSystemDirectory(szSystemDir.GetBuffer(0), MAX_PATH);
szSystemDir.TrimLeft();
szSystemDir.TrimRight();
return szSystemDir;
}
the dir is returned correct, but i can't trim the string to add
an apps filename
eg. CString m_Nfile;
m_Nfile = GetSytemDir();
m_Nfile += "\\app.exe";
AfxMessageBox(m_Nfile);
will return "c:\Winnt\system32" or whatever your flavour
with the .Trim having no effect
if anyone can shed some light, its always appreciated.
shotgun
|
|
|
|
|
You must call ReleaseBuffer() after a GetBuffer(), and before using any other CString methods that modify the string.
--Mike--
Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
thanks alot.
shotgun
|
|
|
|
|
Hi,
As a new beginner I would like to know that: Is it possible with Visual C++ MFC 6.0 I can install an Icon (ShortCut) that links to an application (.exe) in Run Time?
Let say I have:
a) c:\Trial_Application\Trial.exe
b) When I run my SetUp program application Visual C++ 6.0 MFC (Run Time), I would like to add at the user DeskTop an Icon ShortCut (might be the same Icon of the Trial.exe) to that Trial.exe
Some one know how to do that? I appreciate any help here!
Thanks
|
|
|
|
|
|
Hi,
I did try to do the codes, but it failed from the start: the if(SUCCEEDED(hres)) is not TRUE! I do not know why?
Even I try to get in (bypassing if) I get errors the next line (psl->SetDescription(description); ) ... I think I did not now how to use this function! Here what I called:
CreateFileShortcut("Hello","c:\\Try_Icon\\Release",
"c:\\Windows\\Desktop","IconInstallDlg.exe","Trial");
Because I have the c:\Try_Icon\Release\IconInstallDlg.exe & I would like to add an Icon on My Window Desktop ... do you my errors by calling like that?
Would you please to point out what were wrong, I am only the new Visual C++ programmer!
Many thanks,
|
|
|
|
|
Hi all,
I have a dialog that pops up every hour or so, and I'd like it to return to the previous active window after the user clicks OK on the dialog.
I've got a HWND member variable that gets the previous active window in PreCreateWindow, and a SetActiveWindow function in the PostNcDestroy of the popup dialog class. I'm not sure about where else to put these functions, --and it does bomb with an error, "...memory could not be read."
Could someone point me in the right direction? Where's the best place to do this?
Thanks in advance.
Matt
|
|
|
|
|
Found that
::SetForegroundWindow(hwnd); seems to work. Just didn't look deep enough.
Sometimes things are staring you right in the face and you don't even realize it.
Matt
|
|
|
|