Header:
CChristianLifeMinistryHtmlView m_pHtmlView = nullptr;
Source:
m_pHtmlView = new CChristianLifeMinistryHtmlView();
Trying to change it to use a smart pointer. I can do this (inside OnInitDialog):
auto m_pHtmlView2 = std::make_unique<CChristianLifeMinistryHtmlView>;
But I can't work out how to have my smart pointer defined as a member variable of my CDialog class. I can't do: std::unique_ptr m_pHtmlView2.
I saw this discussion (Using smart pointers as a class member) and based on that I tried this in the header:
//CChristianLifeMinistryHtmlView *m_pHtmlView;
std::unique_ptr<CChristianLifeMinistryHtmlView> m_pHtmlView;
But that will not compile:
6>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30704\include\memory(3087,1): error C2248: 'CChristianLifeMinistryHtmlView::~CChristianLifeMinistryHtmlView': cannot access protected member declared in class 'CChristianLifeMinistryHtmlView'
6>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\ChristianLifeMinistryHtmlView.h(104): message : compiler has generated 'CChristianLifeMinistryHtmlView::~CChristianLifeMinistryHtmlView' here
6>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\ChristianLifeMinistryHtmlView.h(22): message : see declaration of 'CChristianLifeMinistryHtmlView'
6>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30704\include\memory(3085): message : while compiling class template member function 'void std::default_delete<CChristianLifeMinistryHtmlView>::operator ()(_Ty *) noexcept const'
6> with
6> [
6> _Ty=CChristianLifeMinistryHtmlView
6> ]
6>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30704\include\memory(3195): message : see reference to function template instantiation 'void std::default_delete<CChristianLifeMinistryHtmlView>::operator ()(_Ty *) noexcept const' being compiled
6> with
6> [
6> _Ty=CChristianLifeMinistryHtmlView
6> ]
6>C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.30.30704\include\memory(3122): message : see reference to class template instantiation 'std::default_delete<CChristianLifeMinistryHtmlView>' being compiled
6>D:\My Programs\2022\MeetSchedAssist\Meeting Schedule Assistant\AvailableBrothersReportPreview.h(52): message : see reference to class template instantiation 'std::unique_ptr<CChristianLifeMinistryHtmlView,std::default_delete<CChristianLifeMinistryHtmlView>>' being compiled
Update
Based on the advice in the comments I now have:
Header:
std::unique_ptr<CChristianLifeMinistryHtmlView> m_pHtmlView;
Source (in OnInitDialog):
m_pHtmlView = std::make_unique<CChristianLifeMinistryHtmlView>();
if (m_pHtmlView != nullptr)
{
m_pHtmlView->Create(nullptr, nullptr, AFX_WS_DEFAULT_VIEW,
m_rctPreviewHtml, this, 0);
m_pHtmlView->ShowWindow(SW_SHOWNORMAL);
if(CMeetingScheduleAssistantApp::WaitForFileToBeReady(m_strTempHtmlFile))
m_pHtmlView->Navigate2(m_strTempHtmlFile, 0, nullptr);
}
It complies and works. My popup dialog displays and the CHtmlView derived control is visible. Cool. But when I click OK to close the dialog I get an exception:
How do we address that?
CHtmlView is derived from CFormView -> CView, which deletes itself in CView::PostNcDestroy with delete this; So the memory is already managed.
Replace new with std::make_unique, and call release() immediately, because you don't want unique_ptr to delete it anymore.
//m_pHtmlView = new CChristianLifeMinistryHtmlView();
m_pHtmlView = std::make_unique<CChristianLifeMinistryHtmlView>().release();
If you had written new and delete in your original code (that also means you at least override CMyHtmlView::PostNcDestroy) then unique_ptr can be used to replace both new and delete.
In this case, you only had new in the original code. You don't want unique_ptr to manage delete
Related
Probably a dumb question (WinRT noob), but, here is goes...
The code from "App.xaml.h" is:
namespace winrt::Precog::implementation
{
struct App : AppT<App>
{
App();
void OnLaunched(Microsoft::UI::Xaml::LaunchActivatedEventArgs const&);
private:
std::wstring cfgDatabase = L"";
winrt::Microsoft::UI::Xaml::Window window{ nullptr };
IAsyncAction loadSettings();
};
}
When I try to compile, Visual Studio gives me a C3646 (unknown override specifier) at the IAsyncAction declaration?
The loadSettings implementation is:
IAsyncAction App::loadSettings()
{
PHKEY regKey = NULL;
LSTATUS regResult;
regResult = RegCreateKey(HKEY_CURRENT_USER, L"Precog", regKey);
if (regResult != ERROR_SUCCESS)
{
ContentDialog errorDialog = ContentDialog();
errorDialog.Title(box_value(L"Erro"));
errorDialog.Content(box_value(L"Pateta"));
errorDialog.CloseButtonText(L"Ok");
errorDialog.XamlRoot(window.Content().XamlRoot());
auto result = co_await errorDialog.ShowAsync();
}
else
{
co_return;
}
}
EDIT: Full compiler output:
Build started...
1>------ Build started: Project: Precog, Configuration: Debug x64 ------
1>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(2304,5): warning MSB3106: Assembly strong name "C:\Code\Precog\packages\Microsoft.WindowsAppSDK.1.0.0\build\native\..\..\lib\uap10.0\Microsoft.Windows.System.winmd" is either a path which could not be found or it is a full assembly name which is badly formed. If it is a full assembly name it may contain characters that need to be escaped with backslash(\). Those characters are Equals(=), Comma(,), Quote("), Apostrophe('), Backslash(\).
1>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(2304,5): warning MSB3106: Assembly strong name "C:\Code\Precog\packages\Microsoft.WindowsAppSDK.1.0.0\build\native\..\..\lib\uap10.0\Microsoft.Windows.PushNotifications.winmd" is either a path which could not be found or it is a full assembly name which is badly formed. If it is a full assembly name it may contain characters that need to be escaped with backslash(\). Those characters are Equals(=), Comma(,), Quote("), Apostrophe('), Backslash(\).
1>App.xaml.cpp
1>C:\Code\Precog\Precog\Precog\App.xaml.h(17,22): error C3646: 'loadSettings': unknown override specifier
1>C:\Code\Precog\Precog\Precog\App.xaml.h(17,34): error C2059: syntax error: '('
1>C:\Code\Precog\Precog\Precog\App.xaml.h(17,36): error C2238: unexpected token(s) preceding ';'
1>C:\Code\Precog\Precog\Precog\App.xaml.cpp(35,19): error C2039: 'loadSettings': is not a member of 'winrt::Precog::implementation::App'
1>C:\Code\Precog\Precog\Precog\App.xaml.h(7): message : see declaration of 'winrt::Precog::implementation::App'
1>C:\Code\Precog\Precog\Precog\App.xaml.cpp(48,30): error C2065: 'window': undeclared identifier
1>Done building project "Precog.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Got it. It seemed to be a namespace mismatch, since changing the declaration to:
winrt::Windows::Foundation::IAsyncAction loadSettings();
Solved the problem. The interesting bit is that Intellisense does not catch the error (no red line underneath the type), only the compiler does.
I try to create a singleton object using shared_ptrs. However the code does not compile when the constructors/destructor are private for the specific object
The code is below.h
//ThreadPool.h
class ThreadPool
{
public:
static std::shared_ptr<ThreadPool> & getInstance();
inline static std::shared_ptr<ThreadPool> m_threadPoolInstance;
private:
ThreadPool() =default;
~ ThreadPool() = default;
ThreadPool(ThreadPool const &) = default;
};
//ThreadPool.cpp
#include "pch.h"
#include <ThreadPool.h>
std::shared_ptr<ThreadPool> & ThreadPool::getInstance()
{
if (! m_threadPoolInstance)
{
ThreadPool * p_ThreadPool = new ThreadPool();
m_threadPoolInstance.reset(p_ThreadPool);
}
return m_threadPoolInstance;
}
I am using VS17 compiler
The error that is created is the following
error C2440: '': cannot convert from '_Ux *' to
'std::shared_ptr'
with
[
_Ux=ThreadPool
] include\memory(1462): note: No constructor could take the source type, or constructor overload resolution was ambiguous
threadpool.cpp(9): note: see reference to function template
instantiation 'void std::shared_ptr::reset(_Ux
*)' being compiled
with
[
_Ux=ThreadPool
] threadpool.cpp(9): note: see reference to function template instantiation 'void std::shared_ptr::reset(_Ux
*)' being compiled
with
[
_Ux=ThreadPool
]
When I set the constructors/destructor in public section, the compilation is succesfull.
However running the same code using gcc compiler , compiles succesfully
The conversion fails because your ThreadPool class has a private destructor.
Calling .reset(ptr) will use the delete expression (delete ptr;) as the deleter, which requires that the destructor be public.
Refer to overload (2) here: https://en.cppreference.com/w/cpp/memory/shared_ptr/reset
2-4) Replaces the managed object with an object pointed to by ptr. Y must be a complete type and implicitly convertible to T. Additionally:
2) Uses the delete expression as the deleter. A valid delete expression must be available, i.e. delete ptr must be well formed, have well-defined behavior and not throw any exceptions. Equivalent to shared_ptr(ptr).swap(*this);.
You either need to make the destructor public or provide a custom deleter.
I am trying to create a vector with a class as its template which has a std::thread member. However, I am not sure on how to properly create the initialization list using the thread. What I have currently is this:
class someclass
{
public:
std::thread thread;
int id;
someclass(std::thread init_thread, int init_id) :
thread(&init_thread),
id(init_id)
{}
};
However, when I try to compile it in VS2012, I get the following error:
f:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1152): error C2064: term does not evaluate to a function taking 0 arguments
which point to the line:
_VARIADIC_EXPAND_0X(_CLASS_BIND, , , , )
If I remove the & from the thread(&init_thread) initialization I get those errors instead:
1>f:\users...\project\source.cpp(43): error C2248: 'std::thread::thread' : cannot access private member declared in class 'std::thread'
1> f:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(73) : see declaration of 'std::thread::thread'
1> f:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(32) : see declaration of 'std::thread'
So, my question is: How would I correctly created such an initialization list?
Later on in the code I doing the following as well (just for reference...)
void function()
{
// ....
std::vector<someclass> v_someclass;
v_someclass.push_back(someclass((std::thread(session, socket)),id));
// ....
}
There are two problems in your code. The most important one is that std::thread is not copy-constructable, but only move-copy constructable. You can make this intent clear by having an rvalue reference as parameter, and you have to use std::move when constructing the thread data member. Second, you should not take the address of the parameter. Your data member is not a pointer.
someclass(std::thread&& init_thread, int init_id) :
thread(std::move(init_thread)),
id(init_id)
This means you can only construct someclass using an std::thread rvalue. For example
someclass sc0(std::thread(args), 42);
std::thread t(args);
someclass sc1(std::move(t), 42);
Edit Note that you can actually have a value parameter: someclass(std::thread, int), but the error message you get when passing an lvalue is not as clear as the one you get with the rvalue reference parameter.
I've been using Crypto++ with VS2005 and VS2010 for a while now. But recently I needed to use it with and application directly. The same code compiles fine when I'm compiling as a DLL and does not compile when compiling as an application.
This is the smallest sample that reproduces the error is this (based on cryptopp561\algparam.h:301 CryptoPP::AlgorithmParametersTemplate
class Base
{
protected:
virtual void MoveInto(void *p) const = 0;
};
template<class T>
class Test: public Base
{
public:
void MoveInto(void * buffer) const
{
Test<T> *x = new(buffer) Test<T>(*this);
}
};
extern template class Test<bool>;
The compilation parameters are the same, only difference that I saw was the configuration type in the project ("Application (.exe)" generates the error and "Dynamic Library (.dll)" does not).
This is the compiler error:
main.h(15): error C2061: syntax error : identifier 'buffer'
main.h(14) : while compiling class template member function 'void Test<T>::MoveInto(void *) const'
with
[
T=bool
]
main.h(20) : see reference to class template instantiation 'Test<T>' being compiled
with
[
T=bool
]
It seems to occur only when theres inheritance. Ommiting : public Base in the class Test declaration makes the error go away.
EDIT:
The problem was in a header included somewhere that defined a a debug version for operator new but didn't declared the placement new version.
Did you #include <new>, the header file that declares placement-new?
Funnily, extern templates are to tell the compiler to not instantiante at some point, so the second error does not make sense to me. Are you certain your compiler has support for extern templates? What if you do the opposite, explicit instantiation:
template class Test<bool>;
First of all, thank you very much for taking the time to look at my question. Second, I have read this and my class does not have any virtual methods I am forgetting to include. I'll go over additional things I have tried after I describe my problem.
I am getting an undefined reference to `vtable for SubcomponentTypeWidget' error when I build my code using gcc version 3.4.6 20060404. Visual Studio 2005 has no issues. I love Linux, but my current political situation has delegated it to the red headed step child that regularly suffers abuse. Hopefully by our powers combined, I can remedy that.
I am using Qt version 4.6.2. I am using gcc 3.4.6 20060404 on Red Hat 4.
This is my header:
#ifndef SubcomponentTypeWidget_h
#define SubcomponentTypeWidget_h
#include <vector>
#include "ui_SubcomponentTypeWidget.h"
#include "Subcomponent.h"
class SubcomponentTypeWidget : public QWidget, public Ui::SubcomponentTypeWidget
{
Q_OBJECT
Q_PROPERTY(QString title READ title WRITE setTitle)
public:
SubcomponentTypeWidget(QWidget* parent,
Models::Subcomponent::SubcomponentType subcomponentType = Models::Subcomponent::kSolid)
: QWidget(parent),
m_subcomponentType(subcomponentType),
m_subcomponentTypeButtonGroup(new QButtonGroup(this))
{
this->initialize();
}
Models::Subcomponent::SubcomponentType subcomponentType() const { return m_subcomponentType; }
void setSubcomponentType(Models::Subcomponent::SubcomponentType type);
void setNonCompatibleTypes(const std::vector<Models::Subcomponent::SubcomponentType>& types);
QString title() const { return m_subcomponentGroupBox->title(); }
void setTitle(const QString &title) { m_subcomponentGroupBox->setTitle(title); }
signals:
void subcomponentTypeChanged();
protected slots:
void handleSubcomponentTypeChoice(int subcomponentTypeChoiceId);
protected:
void initialize();
Models::Subcomponent::SubcomponentType m_subcomponentType;
QButtonGroup* m_subcomponentTypeButtonGroup;
};
#endif // SubcomponentTypeWidget_h
The implementation is here:
#include "SubcomponentTypeWidget.h"
void SubcomponentTypeWidget::setSubcomponentType(Models::Subcomponent::SubcomponentType type)
{
if (type != m_subcomponentType)
{
m_subcomponentType = type;
emit subcomponentTypeChanged();
}
}
void SubcomponentTypeWidget::setNonCompatibleTypes(const std::vector<Models::Subcomponent::SubcomponentType>& types)
{
m_subcomponentTypeButtonGroup->button(static_cast<int>(Models::Subcomponent::kSolid) + 1)->setEnabled(true);
m_subcomponentTypeButtonGroup->button(static_cast<int>(Models::Subcomponent::kComplement) + 1)->setEnabled(true);
m_subcomponentTypeButtonGroup->button(static_cast<int>(Models::Subcomponent::kHole) + 1)->setEnabled(true);
for (std::vector<Models::Subcomponent::SubcomponentType>::const_iterator it = types.begin(); it != types.end(); ++it)
{
m_subcomponentTypeButtonGroup->button(static_cast<int>(*it) + 1)->setEnabled(false);
if (*it == m_subcomponentType)
m_subcomponentTypeButtonGroup->button(static_cast<int>(Models::Subcomponent::kSolid) + 1)->setChecked(true);
}
}
void SubcomponentTypeWidget::handleSubcomponentTypeChoice(int subcomponentTypeChoiceId)
{
if (static_cast<Models::Subcomponent::SubcomponentType>(subcomponentTypeChoiceId - 1) != m_subcomponentType)
{
m_subcomponentType = static_cast<Models::Subcomponent::SubcomponentType>(subcomponentTypeChoiceId - 1);
emit subcomponentTypeChanged();
}
}
void SubcomponentTypeWidget::initialize()
{
this->setupUi(this);
m_subcomponentTypeButtonGroup->addButton(m_solidRadioButton, static_cast<int>(Models::Subcomponent::kSolid) + 1);
m_subcomponentTypeButtonGroup->addButton(m_complementRadioButton, static_cast<int>(Models::Subcomponent::kComplement) + 1);
m_subcomponentTypeButtonGroup->addButton(m_holeRadioButton, static_cast<int>(Models::Subcomponent::kHole) + 1);
m_subcomponentTypeButtonGroup->button(static_cast<int>(m_subcomponentType) + 1)->setChecked(true);
connect(m_subcomponentTypeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(handleSubcomponentTypeChoice(int)));
}
The actual error messages I am receiving are:
../lib/libMeshAndGeometry.a(AddAdvancedDialog.o)(.gnu.linkonce.t._ZN20Ui_AddAdvancedDialog7setupUiEP7QDialog+0x955): In function `Ui_AddAdvancedDialog::setupUi(QDialog*)':
: undefined reference to `vtable for SubcomponentTypeWidget'
../lib/libMeshAndGeometry.a(AddAdvancedDialog.o)(.gnu.linkonce.t._ZN20Ui_AddAdvancedDialog7setupUiEP7QDialog+0x960): In function `Ui_AddAdvancedDialog::setupUi(QDialog*)':
: undefined reference to `vtable for SubcomponentTypeWidget'
../lib/libMeshAndGeometry.a(AddAdvancedDialog.o)(.gnu.linkonce.t._ZN20Ui_AddAdvancedDialog7setupUiEP7QDialog+0x99b): In function `Ui_AddAdvancedDialog::setupUi(QDialog*)':
: undefined reference to `SubcomponentTypeWidget::initialize()'
../lib/libMeshAndGeometry.a(AddBoxDialog.o)(.gnu.linkonce.t._ZN15Ui_AddBoxDialog7setupUiEP7QDialog+0xfe8): In function `Ui_AddBoxDialog::setupUi(QDialog*)':
: undefined reference to `vtable for SubcomponentTypeWidget'
../lib/libMeshAndGeometry.a(AddBoxDialog.o)(.gnu.linkonce.t._ZN15Ui_AddBoxDialog7setupUiEP7QDialog+0xff3): In function `Ui_AddBoxDialog::setupUi(QDialog*)':
: undefined reference to `vtable for SubcomponentTypeWidget'
../lib/libMeshAndGeometry.a(AddBoxDialog.o)(.gnu.linkonce.t._ZN15Ui_AddBoxDialog7setupUiEP7QDialog+0x102e): In function `Ui_AddBoxDialog::setupUi(QDialog*)':
: undefined reference to `SubcomponentTypeWidget::initialize()'
../lib/libMeshAndGeometry.a(AddConeDialog.o)(.gnu.linkonce.t._ZN16Ui_AddConeDialog7setupUiEP7QDialog+0x7ef): In function `Ui_AddConeDialog::setupUi(QDialog*)':
: undefined reference to `vtable for SubcomponentTypeWidget'
../lib/libMeshAndGeometry.a(AddConeDialog.o)(.gnu.linkonce.t._ZN16Ui_AddConeDialog7setupUiEP7QDialog+0x7fa): In function `Ui_AddConeDialog::setupUi(QDialog*)':
: undefined reference to `vtable for SubcomponentTypeWidget'
../lib/libMeshAndGeometry.a(AddConeDialog.o)(.gnu.linkonce.t._ZN16Ui_AddConeDialog7setupUiEP7QDialog+0x835): In function `Ui_AddConeDialog::setupUi(QDialog*)':
: undefined reference to `SubcomponentTypeWidget::initialize()'
../lib/libMeshAndGeometry.a(AddCylinderDialog.o)(.gnu.linkonce.t._ZN20Ui_AddCylinderDialog7setupUiEP7QDialog+0x9c4): In function `Ui_AddCylinderDialog::setupUi(QDialog*)':
: undefined reference to `vtable for SubcomponentTypeWidget'
../lib/libMeshAndGeometry.a(AddCylinderDialog.o)(.gnu.linkonce.t._ZN20Ui_AddCylinderDialog7setupUiEP7QDialog+0x9cf): In function `Ui_AddCylinderDialog::setupUi(QDialog*)':
: undefined reference to `vtable for SubcomponentTypeWidget'
../lib/libMeshAndGeometry.a(AddCylinderDialog.o)(.gnu.linkonce.t._ZN20Ui_AddCylinderDialog7setupUiEP7QDialog+0xa0a): In function `Ui_AddCylinderDialog::setupUi(QDialog*)':
All of my makefiles have been generated from my .pro file using qmake. The main make files, the main .pro file, and the widgets and MeshAndGeometry make and .pro files are attached here as an archive. One of the .ui files is attached here.
I have tried a number of things.
I am sure this is not a stale object file issue. I have built this from scratch and I still have the the problem.
I checked all of my capitalization issues. One of the problems I have noticed with doing most of this on Windows and then moving it to Linux is that people make mistakes with capitalization, and Windows doesn't care about capitalization.
I ran nm -a -C SubcomponentTypeWidget.o to see if the necessary vtable was there, and it wasn't. However, the 'missing' methods were there.
Creating a virtual destructor does not force vtable generation for SubcomponentTypeWidget.
I have tried removing large chunks of SubcomponentTypeWidget's functionality. This results in removing the specific linker error messages for methods, but it does not remove the undefined reference to vtable message. I have removed everything from SubcomponentTypeWidget other than the constructor, and in this case I still receive the "undefined reference to vtable" message, but without any mention to specific methods we are looking for.
Changing the order of which widgets and MeshingAndGeometry are linked in does not help.
I have tried gcc versions 3.4.6 20060404 and 4.1.2 20080704.
Help me, Obi-Wan Kenobi. You're my only hope.
Thank you all very, very much,
-Brian J. Stinar-
went through your codes the error most likely arises from the following statement:
=======================================================
void SubcomponentTypeWidget::initialize()
{
this->setupUi(this);
//rest of codes
}
=======================================================
you are sort of breaking the structure of Qt by making SubcomponentTypeWidget a subclass of Ui::SubcomponentTypeWidget. You are actually using yourself to setup a UI of yourself in this case. Coupled that with the fact that you are using multiple class inheritance, you are just confusing the compiler on which virtual method to refer to during runtime.
Instead of subclassing Ui::SubcomponentTypeWidget, just make it a private variable in SubcomponentTypeWidget
//SubcomponentTypeWidget.h
private:
Ui::SubcomponentTypeWidge ui;
implement the following in your init function and you should be good to go
void SubcomponentTypeWidget::initialize()
{
ui.setupUi(this);
//rest of codes
}
My problem was with solution point number six. I actually did NOT change this order correctly. I was changing the order in the INCPATH instead of the order in the LIBS.
After adding the line MeshAndGeometry.depends = widgets to my master.pro file, running qmake, and running make, this problem went away.
Thank everyone very much for their comments and help.
-Brian J. Stinar-