|
I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?
Quote: e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
1> with
1> [
1> _Ty=int
1> ]
1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
1> with
1> [
1> _Ty=int
1> ]
1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.28
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
here is my code:
#include <iostream>
#include <memory>
#include <vector>
#include <map>
struct Baa {
std::unique_ptr<int> x;
Baa() {
}
Baa(Baa&& o)
{
x = std::move(o.x);
}
Baa operator = (Baa&& param)
{
x = std::move(param.x);
return *this;
}
};
int main()
{
std::map<int, Baa> sheep;
Baa ba;
sheep[0] = ba;
}
|
|
|
|
|
which compiler you use? with what options?
|
|
|
|
|
|
the problem is simple. The code
sheep[0] = ba; should be
sheep[0] = std::move(ba); Your first approach used the copy constructor to copy the unique_ptr this defeats the purpose unique_ptr.
Also another issue is:
Baa operator = (Baa&& param) should be
Baa& operator = (Baa&& param)
|
|
|
|
|
Visual Studio 2008, MFC, C++, Windows 7 and Windows XP
My project uses WCHAR and a dialog needs a CListBox. But CListBox does not like wchar_t or WCHAR. In particular, method AddString(). My searches have not discovered a CListBoxW or anything that looks like wchar_t methods. What is the proper control to use that performs the CListBox operations?
I see that CString will work, but then the WCHAR must be converted to CString. Must that path be taken?
Resolution: I was certain Unicode was selected. I was wrong, MBCS was selected. Changing that to Unicode resolved the apparent inconsistencies.
Thank you for your time
modified 29-May-15 9:59am.
|
|
|
|
|
Change the Project Settings to build Unicode build. This will make CString and CListBox become Unicode
|
|
|
|
|
According to the CListBox documentation[^], the AddString method takes a LPCTSTR parameter; that is, a pointer to a TCHAR[] . If your project is built for Unicode that will be equated to LPCWSTR which is a pointer to a WCHAR[] .
|
|
|
|
|
Hello,
My project is MFC and does and did have Unicode set. Right now, things are rather funky and I don't understand. For example
CString a;
CString b;
b = "test";
a.Format( "%s", b ); // does not compile.
a.Format( L"%s", b ); // does compile.
A search found a tutorial that shows use without the L prefix.
So I went back to the WCHAR concept and now it works.
I really wish I was not the only one here working with C++.
Thank you for taking the time to reply.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
If you are working in Unicode the the Format statement requires that the format string be declared as Unicode, with either the L prefix or TEXT() macro, thus:
a.Format( L"%s", b ); a.Format( TEXT("%s"), b );
Also, you should use upper case S to indicate that the contents of b is actually an ASCII, rather than Unicode, string.
bkelly13 wrote: I really wish I was not the only one here working with C++. You are not, if you look in the C/C++ forum you will see fairly regular questions.
|
|
|
|
|
Hello Richard,
This morning I determined that some code using CString compiled in Debug mode but not in Release mode. That was really puzzling. After messing around I discovered that I had multibyte character set enabled rather than Unicode. It explained some other problems I had since I was sure Unicode was selected. Now I am back on a path of progress.
I was not clear in my last statement of my previous post. I really wish I was not the only one at this work site working with C++. There is an Oracle data base guy and five people working on a specialized script for processing telemetry data. Most of them know C# and they disdain C++.
I'll stick with Unicode.
I am thinking of dropping WCHAR and going to wchar_t. Is that a good or bad idea? Or maybe indifferent?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
bkelly13 wrote: and they disdain C++. Thanks for clarifying Bryan; seems you really are the one with the tough part of the job.
bkelly13 wrote: dropping WCHAR and going to wchar_t Indifferent really, as WCHAR is just a typedef for wchar_t , probably a hangover from the days before C++. I know that going from ASCII to Unicode is initially a bit of a pain, but it is probably the right decision for the long term.
|
|
|
|
|
|
What?!
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I have added dual interfaces to an existing class in MFC application but when i run the client the cocreateinstance is failing with the error 0x80080005 (CO_E_SERVER_EXEC_FAILURE) Server execution failed. I had followed the steps from the articles below:-
http://www.codeproject.com/Articles/8202/Adding-automation-to-MFC-applications.
https://msdn.microsoft.com/en-us/library/4h56szat.aspx[^]
Hide Copy Code
hr = ::CoCreateInstance(CLSID_Application, NULL, CLSCTX_LOCAL_SERVER, IID_IApplication, (void**)&m_IApplication);
if (SUCCEEDED(hr)) {
if (m_IApplication)
{
m_IApplication->Show();
}
}
|
|
|
|
|
Check the even log for the details.
|
|
|
|
|
As topic says, I'm having some HItTest issue.
Background:
I'm making a windows mobile ce 6.5 application.
There is some resource based dialog with ListView on it.
I'm subclassing the ListView with "CustomLVCtrl" and HeaderCtrl with "CustomHCtrl".
Both custom controls have increased height based on some constant value.
Now about my problem:
From WM_GESTURE GID_HOLD handler i'm getting some screen coordinates and then testing if any ListView item is being hold.
The problem is that i'm never getting a valid item index. It looks like item with index 0 is under the header, because testing first item results in 1, second returns 2, ..., then last returns -1. These results are not accurate for the whole item height either.
Simplified code:
void Controller::onHold(int x, int y)
{
CPoint point(x, y);
view_->list_.ScreenToClient(&point);
LONG dlgBaseUnits = GetDialogBaseUnits();
int baseunitX = LOWORD(dlgBaseUnits), baseunitY = HIWORD(dlgBaseUnits);
point.x = ATL::MulDiv(point.x, 4, baseunitX);
point.y = ATL::MulDiv(point.y, 8, baseunitY);
LVHITTESTINFO lvhti;
lvhti.flags = 0;
lvhti.pt = point;
int nItem = view_->list_.HitTest(&lvhti);
if (lvhti.flags & LVHT_ONITEM)
{
ShowModal(nItem);
}
}
I've also tried with
RECT rect={0,0,4,8};
MapDialogRect(hwnd, &rect);
int baseunitX = rect.right;
int baseunitY = rect.bottom;
point.x = ATL::MulDiv(point.x, 4, baseunitX);
point.y = ATL::MulDiv(point.y, 8, baseunitY);
but with same results. Can anyone point out what I'm doing wrong?
|
|
|
|
|
Visual Studio 2008, C++, MFC
lang="cs">
const char build_date[] = __DATE__;
swprintf_s( my_date, my_size, L"%s", build_date );
swprintf_s( my_date, my_size, L"%ls, build_date );
show_date.SetWindowTextW( my_date );
show_date.SetWindowText( __DATE__
show_date.SetWindowText( L"this displays" );
String my_date does not contain the date that is found in build_date. Multiple versions have been tried and not successful in getting compile date to display in the dialog. All the searches I have done tell me this should work.
What must be changed?
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 24-Apr-15 8:02am.
|
|
|
|
|
You are using the wide character version of swprintf_s , but your date string is char type, so you need to use the upper case S in your format string so it gets converted to Unicode, thus:
swprintf_s( my_date, my_size, L"%S", build_date );
|
|
|
|
|
That was it. Thank you.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
This application has several pointers to be deleted in the destructor. Each is logged when created and again when deleted to reduce the possibility of an undetected memory leak. As there are several, a function is needed to do the deletes. The goal is to avoid writing all that logging code multiple times. Below is the concept. The first time it was tried the subject was an instance of a class with its own destructor. A breakpoint was placed in that destructor.
Void Delete_Stuff( WCHAR * name, void * item )
{
If( item != NULL )
{
Write_Log_Entry( L"Preparing to delete <name> at address <*item>"); // please see note
delete ( item ); // does not call the destructor
delete ( *item ); // illegal indirection
}
}
EDIT: I wrote the text up in MS word and did not notice that the leading character of "delete" had been capitalized. Apologies for that.
Note: An swprintf_s(…) is used to build the string and the named function does the logging. Please accept the gross simplification used to make the main point obvious.
The destructor for the object is not called. I have tried a few combinations including * *. I managed to corrupt the heap at least once. Can this be fixed and work as desired or must each item be deleted one at a time without using the Delete_Stuff( … ) function.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 24-Apr-15 8:02am.
|
|
|
|
|
|
It stepped through the delete but the destructor for that class/object was not called.
Note: I wrote the text up in MS word and did not notice that the leading character of "delete" had been capitalized. Apologies for that.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
|
|
|
|
|
OK, but what is your Delete method actually doing?
|
|
|
|
|
I do not have a delete function. The parens were used in an attempt to tell the standard delete operation that I want to delete the object pointed to by the pointer. An indirect delete rather than a direct delete.
class widget{ ... }
p_widget = new widget;
...
delete_stuff( L"widget", p_widget );
...
void delete_stuff( WCHAR * name, void * item )
{
delete *item;
... logging code not shown.
}
The destructor of widget has a breakpoint that was never hit. I presume that widget was not deleted.
Because that did not work, I tried dereferencing and grouping the pointer as in:
delete *item;
and
delete (*item);
I am unable to get the breakpoint in the destructor so presume I am doing something wrong.
Thank you for your time
If you work with telemetry, please check this bulletin board: www.irigbb.com
modified 22-Apr-15 15:25pm.
|
|
|
|
|
I see the problem; my bad for missing it in your original question. Your function has the signature:
Void Delete_Stuff( WCHAR * name, void * item )
so when you call delete item the compiler has no idea what type item is, so just throws it away. You can only call delete on an object, when it is specifically typed so the compiler knows how to deal with it. You may try casting it to the relevant class type, but you would need some way of telling your function what type each pointer is.
|
|
|
|