rapidjson object as function argument cause compiler error - reference

I try to pass rapidjson::Document object as function argument:
std::string json_to_string(rapidjson::Document jmsg)
{
// Convert JSON document to string
rapidjson::StringBuffer buffer;
rapidjson::Writer< rapidjson::StringBuffer > writer(buffer);
jmsg.Accept(writer);
std::string str = buffer.GetString();
return str;
}
If I do the function just as above, I got this error when I compile the code:
In function `rapidjson::GenericDocument, rapidjson::MemoryPoolAllocator >::GenericDocument(rapidjson::GenericDocument, rapidjson::MemoryPoolAllocator > const&)':
../../rapidjson/document.h:691: undefined reference to `rapidjson::GenericValue, rapidjson::MemoryPoolAllocator >::GenericValue(rapidjson::GenericValue, rapidjson::MemoryPoolAllocator > const&)'
collect2: error: ld returned 1 exit status
The error disappears if I change the argument type from "rapidjson::Document jmsg" to "rapidjson::Document &jmsg". Use the reference is ok, however, I still want to know what's wrong with the code if I don't define it as a reference type.

You cannot pass a Document as value, you must pass it by reference or pointer.
This is because Document is not copy-able.
I suggest this function declaration in your situation:
std::string json_to_string(const rapidjson::Document& jmsg)

Related

Python type hint for undefined values to distinguish from None

I want to create a function signature where an undefined value and None are distinct from each other.
So e.g.:
undefined = object()
def update(id: str, title: str = undefined, description: str):
data = {
"title": title,
"description": description,
}
// remove undefined key-value pairs from data
for key in data.keys():
if data[key] is undefined:
del data[key]
# send data to the server for further processing
Obviously this way I get conflicting types and the following error: Incompatible default for argument "description" (default has type "object", argument has type "str")
From the above example it can be seen that passing None is completely valid as it will be replaced as the null value of JSON while if undefined is passed then the value won't be considered in the data.
I've tried to define undefined types like:
UndefinedStr = typing.Union[str, undefined]
But I got the following error: Variable "undefined" is not valid as a type
I guess that's because undefined is an instance. So as a workaround I could do:
class Undefined:
pass
undefined = Undefined()
UndefinedStr = Union[str, Undefined]
This solution works, but to me feels a bit too broad.
So my question what is the best way to type hint this undefined use-case?

Cannot link an application with libavcodec

I'm trying to make a simplest app with usage of ffmpeg libs.
Here is my code:
main.c:
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
int main()
{
avformat_network_init();
return 0;
}
compiler command:
gcc -o main main.c -Wall -lavformat -lavcodec -lswresample -lswscale -lavutil -lm -lz -lpthread -L/usr/local/lib/
leads to :
/usr/local/lib//libavformat.a(matroskaenc.o): In function
get_aac_sample_rates':
/home/ievdokimov/repos/FFmpeg/libavformat/matroskaenc.c:709: undefined
reference toavpriv_mpeg4audio_get_config2'
/home/ievdokimov/repos/FFmpeg/libavformat/matroskaenc.c:709: undefined
reference to avpriv_mpeg4audio_get_config2'
/usr/local/lib//libavformat.a(mux.o): In function
ff_interleave_add_packet':
/home/ievdokimov/repos/FFmpeg/libavformat/mux.c:936: undefined
reference to av_packet_make_refcounted'
/usr/local/lib//libavformat.a(utils.o): In function
ff_packet_list_put':
/home/ievdokimov/repos/FFmpeg/libavformat/utils.c:461: undefined
reference to av_packet_make_refcounted'
/usr/local/lib//libavformat.a(utils.o): In functionparse_packet':
/home/ievdokimov/repos/FFmpeg/libavformat/utils.c:1494: undefined
reference to av_packet_make_refcounted'
/usr/local/lib//libavformat.a(utils.o): In functionff_read_packet':
/home/ievdokimov/repos/FFmpeg/libavformat/utils.c:876: undefined
reference to av_packet_make_refcounted'
/usr/local/lib//libavformat.a(codec2.o): In function
codec2_read_header_common':
/home/ievdokimov/repos/FFmpeg/libavformat/codec2.c:74: undefined
reference to avpriv_codec2_mode_bit_rate'
/home/ievdokimov/repos/FFmpeg/libavformat/codec2.c:75: undefined
reference toavpriv_codec2_mode_frame_size'
/home/ievdokimov/repos/FFmpeg/libavformat/codec2.c:76: undefined
reference to avpriv_codec2_mode_block_align'
/home/ievdokimov/repos/FFmpeg/libavformat/codec2.c:74: undefined
reference toavpriv_codec2_mode_bit_rate'
/home/ievdokimov/repos/FFmpeg/libavformat/codec2.c:75: undefined
reference to avpriv_codec2_mode_frame_size'
/home/ievdokimov/repos/FFmpeg/libavformat/codec2.c:76: undefined
reference toavpriv_codec2_mode_block_align'
/usr/local/lib//libavformat.a(isom.o): In function
ff_mp4_read_dec_config_descr':
/home/ievdokimov/repos/FFmpeg/libavformat/isom.c:552: undefined
reference toavpriv_mpeg4audio_get_config2'
/usr/local/lib//libavformat.a(latmenc.o): In function
latm_decode_extradata':
/home/ievdokimov/repos/FFmpeg/libavformat/latmenc.c:65: undefined
reference toavpriv_mpeg4audio_get_config2'
/home/ievdokimov/repos/FFmpeg/libavformat/latmenc.c:65: undefined
reference to avpriv_mpeg4audio_get_config2'
/usr/local/lib//libavformat.a(matroskadec.o): In function
matroska_decode_buffer':
/home/ievdokimov/repos/FFmpeg/libavformat/matroskadec.c:1657:
undefined reference to BZ2_bzDecompressInit'
/home/ievdokimov/repos/FFmpeg/libavformat/matroskadec.c:1672:
undefined reference toBZ2_bzDecompress'
/home/ievdokimov/repos/FFmpeg/libavformat/matroskadec.c:1665:
undefined reference to BZ2_bzDecompressEnd'
/home/ievdokimov/repos/FFmpeg/libavformat/matroskadec.c:1675:
undefined reference toBZ2_bzDecompressEnd'
/usr/local/lib//libavformat.a(mxfenc.o): In function
get_ue_golomb_31':
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:140: undefined
reference toff_ue_golomb_vlc_code'
/usr/local/lib//libavformat.a(mxfenc.o): In function
mxf_parse_dnxhd_frame':
/home/ievdokimov/repos/FFmpeg/libavformat/mxfenc.c:2030: undefined
reference toavpriv_dnxhd_get_hr_frame_size'
/usr/local/lib//libavformat.a(rtpenc_vc2hq.o): In function
init_get_bits_xe':
/home/ievdokimov/repos/FFmpeg/./libavcodec/get_bits.h:628: undefined
reference toff_interleaved_golomb_vlc_len'
/usr/local/lib//libavformat.a(rtpenc_vc2hq.o): In function
get_interleaved_ue_golomb':
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:197: undefined
reference toff_interleaved_dirac_golomb_vlc_code'
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:185: undefined
reference to ff_interleaved_dirac_golomb_vlc_code'
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:197: undefined
reference toff_interleaved_dirac_golomb_vlc_code'
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:197: undefined
reference to ff_interleaved_dirac_golomb_vlc_code'
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:185: undefined
reference toff_interleaved_dirac_golomb_vlc_code'
/usr/local/lib//libavformat.a(rtpenc_vc2hq.o):/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:185:
more undefined references to ff_interleaved_dirac_golomb_vlc_code'
follow /usr/local/lib//libavformat.a(rtpenc_vc2hq.o): In function
get_interleaved_ue_golomb':
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:183: undefined
reference to ff_interleaved_ue_golomb_vlc_code'
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:183: undefined
reference toff_interleaved_ue_golomb_vlc_code'
/home/ievdokimov/repos/FFmpeg/./libavcodec/golomb.h:183: undefined
reference to ff_interleaved_ue_golomb_vlc_code'
/usr/local/lib//libavformat.a(spdifdec.o): In function
spdif_get_offset_and_codec':
/home/ievdokimov/repos/FFmpeg/libavformat/spdifdec.c:63: undefined
reference to av_adts_header_parse'
/home/ievdokimov/repos/FFmpeg/libavformat/spdifdec.c:63: undefined
reference toav_adts_header_parse'
/usr/local/lib//libavformat.a(spdifenc.o): In function
spdif_header_aac':
/home/ievdokimov/repos/FFmpeg/libavformat/spdifenc.c:362: undefined
reference toav_adts_header_parse'
/usr/local/lib//libavformat.a(ac3dec.o): In function ac3_eac3_probe':
/home/ievdokimov/repos/FFmpeg/libavformat/ac3dec.c:63: undefined
reference toav_ac3_parse_header'
/home/ievdokimov/repos/FFmpeg/libavformat/ac3dec.c:60: undefined
reference to av_ac3_parse_header'
/usr/local/lib//libavformat.a(adtsenc.o): In function
adts_decode_extradata':
/home/ievdokimov/repos/FFmpeg/libavformat/adtsenc.c:56: undefined
reference to `avpriv_mpeg4audio_get_config2' collect2: error: ld
returned 1 exit status
Is there any way to fix that?
It seems like the libavformat.sofile version is different from other libraries.
Please check the version compatible with other libraries like liavcodec

Compile error when try to create singleton using shared_ptr

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.

VTable Not Generated

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-

Is it possible to pass an std::string via Microsoft RPC?

I tried passing an std::string via RPC, but I got the following error:
MIDL2025: syntax error: expecting a
type specification or a storage
specifer or a type qualifier near
"string"
Extract from code:
interface TestInterface
{
unsigned int HelloUser([in] const string user);
}
Is this possible?
You must use a BSTR. Also, no const. By specifying the argument as [in], it is already understood that the callee will not modify the string, and even if it did modify, it won't be marshaled back to the caller.
The _bstr_t class will help with conversion. Note that BSTR is always based on WCHAR, which is 16-bit. Thus, use std::wstring.

Resources