How to format pointers using fmt library? - c++

I'm taking up some folks advice and looking into the fmt library:
http://fmtlib.net
It appears to have the features I need, and claims to support %p (pointer), but when compiling my code which uses a %p I get a long string of template errors (incomprehensible). I'll post them at the end of this.
if I pull out the %p and the corresponding pointer argument, then it compiles on VS2017 c++17.
However, I'm at a loss as to how to either decode the template errors, or to get some insight as to why it won't accept a %p argument in the first place.
I've tried casting the argument to a (void*) - same issue.
I've tried using the python style syntax in the formatter {} - same issue.
I've broken out the %p bits separately from the rest of the formatting - same issue.
I see that there is support for user-types - but in this case I just want to output this as a raw pointer value. I could just skip it, after all how valuable can the pointer address be, really? But of course that means more work during conversion from sprintf to fmt::format to hunt down all %p and "do something with them" such as elide them.
But the docs seem to indicate that %p is supported - http://fmtlib.net/latest/syntax.html (about 3/4 of the way down - search on 'pointer' or 'p').
Here's the calling function: (note: pAccels is declared as const ACCEL *)
m_hAccel = ::CreateAcceleratorTable(const_cast<LPACCEL>(pAccels), (int)count);
if (!m_hAccel)
{
auto error = GetLastError();
throw CWinAPIErrorException(__FUNCTION__, "CreateAcceleratorTable", fmt::format("%p,%u", pAccels, count), error);
}
Here's the diagnostic spewage:
1>c:\users\steve\source\fmt\include\fmt\core.h(1073): error C2825: 'fmt::v5::internal::get_type<Context,Arg>::value_type': must be a class or namespace when followed by '::'
1> with
1> [
1> Context=fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<char>>,char>,
1> Arg=const ACCEL *
1> ]
1>c:\users\steve\source\fmt\include\fmt\core.h(1081): note: see reference to class template instantiation 'fmt::v5::internal::get_type<Context,Arg>' being compiled
1> with
1> [
1> Context=fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<char>>,char>,
1> Arg=const ACCEL *
1> ]
1>c:\users\steve\source\fmt\include\fmt\core.h(1190): note: see reference to function template instantiation 'unsigned __int64 fmt::v5::internal::get_types<Context,const ACCEL*,size_t>(void)' being compiled
1> with
1> [
1> Context=fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<char>>,char>
1> ]
1>c:\users\steve\source\fmt\include\fmt\core.h(1190): note: while compiling class template member function 'unsigned __int64 fmt::v5::format_arg_store<fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<Char>>,Char>,const ACCEL *,size_t>::get_types(void)'
1> with
1> [
1> Char=char
1> ]
1>c:\users\steve\source\fmt\include\fmt\core.h(1478): note: see reference to class template instantiation 'fmt::v5::format_arg_store<fmt::v5::basic_format_context<std::back_insert_iterator<fmt::v5::internal::buffer<Char>>,Char>,const ACCEL *,size_t>' being compiled
1> with
1> [
1> Char=char
1> ]
1>c:\users\steve\source\tbx\wapi\acceleratortable.cpp(58): note: see reference to function template instantiation 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> fmt::v5::format<char[6],const ACCEL*,size_t,0>(const S (&),const ACCEL *const &,const size_t &)' being compiled
1> with
1> [
1> S=char [6]
1> ]
1>c:\users\steve\source\fmt\include\fmt\core.h(1073): error C2510: 'value_type': left of '::' must be a class/struct/union
1>c:\users\steve\source\fmt\include\fmt\core.h(1073): error C2065: 'type_tag': undeclared identifier
1>c:\users\steve\source\fmt\include\fmt\core.h(1073): error C2131: expression did not evaluate to a constant
1>c:\users\steve\source\fmt\include\fmt\core.h(1073): note: a non-constant (sub-)expression was encountered
1>c:\users\steve\source\fmt\include\fmt\core.h(1197): error C2131: expression did not evaluate to a constant
1>c:\users\steve\source\fmt\include\fmt\core.h(1082): note: failure was caused by non-constant arguments or reference to a non-constant symbol
1>c:\users\steve\source\fmt\include\fmt\core.h(1082): note: see usage of 'value'

To format a pointer you can either cast it to void*:
std::string s = fmt::format("{},{}", static_cast<void*>(pAccels), count);
or wrap it in fmt::ptr:
std::string s = fmt::format("{},{}", fmt::ptr(pAccels), count);
Working example on godbolt: https://godbolt.org/z/sCNbjr
Note that format uses Python-like format string syntax, not printf's and returns a std::string object.

Related

C++ Boost matrix error when transforming input to classes

First of all, I use boost library, and if it changes anything, the code is compiled on a Windows Machine.
The code itself contains a lot more of function acting upon matrices but only this one triggers the error.
Well, I am trying to transform matrix like :
{001
100
010}
To something like :
{1
3
2}
But strangely I can't compile my code and I can't find the error so I would be glad if anyone could help me.
Below the code :
using namespace boost::numeric::ublas;
typedef matrix <float, row_major, unbounded_array<float>> MATRIXf;
MATRIXf matrix_to_class (const MATRIXf inputM)
{
MATRIXf output;
for (std::size_t line = 0; line < inputM.size1(); line++)
{
for (std::size_t column = 0; column < inputM.size2(); column++)
{
if (column == 1)
{
output.insert_element(line,0.0,column);
}
}
}
return output;
}
Here is the error code:
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): error C4996: 'std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(2372): note: see declaration of 'std::copy::_Unchecked_iterators::_Deprecate'
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(204): note: see reference to function template instantiation '_OutIt *std::copy<float*,float*>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=float *,
1> _InIt=float *
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(201): note: while compiling class template member function 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>> &boost::numeric::ublas::unbounded_array<T,std::allocator<T>>::operator =(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)'
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(310): note: see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>> &boost::numeric::ublas::unbounded_array<T,std::allocator<T>>::operator =(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)' being compiled
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(102): note: see reference to class template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>' being compiled
1> with
1> [
1> T=float
1> ]
1> g:\c++ python\travail\visualstudio\visualstudio\guigui\neural net\neural net\utils.hpp(21): note: see reference to class template instantiation 'boost::numeric::ublas::matrix<float,boost::numeric::ublas::row_major,boost::numeric::ublas::unbounded_array<float,std::allocator<T>>>' being compiled
1> with
1> [
1> T=float
1> ]
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory(102): error C4996: 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate': Call to 'std::uninitialized_copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory(102): note: see declaration of 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate'
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(94): note: see reference to function template instantiation '_FwdIt *std::uninitialized_copy<const float*,float*>(_InIt,_InIt,_FwdIt)' being compiled
1> with
1> [
1> _FwdIt=float *,
1> _InIt=const float *
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\storage.hpp(89): note: while compiling class template member function 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>::unbounded_array(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)'
1> with
1> [
1> T=float
1> ]
1> e:\c++ libraries\general\boost_1_65_0\boost\numeric\ublas\matrix.hpp(162): note: see reference to function template instantiation 'boost::numeric::ublas::unbounded_array<float,std::allocator<T>>::unbounded_array(const boost::numeric::ublas::unbounded_array<T,std::allocator<T>> &)' being compiled
1> with
1> [
1> T=float
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Trying to locate the error brings me to the function above.
Thanks in advance.
Near the end of the first error message is the text, "To disable this warning, use -D_SCL_SECURE_NO_WARNINGS". https://msdn.microsoft.com/en-us/library/ttcz0bys.aspx has a more detailed discussion of warnings and checked iterators. In essence, Microsoft created "safe" mutations of Standard C++ functions to help developers avoid invalid iterator usage. The error message suggests that you define _SCL_SECURE_NO_WARNINGS. This can be done in the project properties C/C++/Preprocessor/Preprocessor Definitions. In a project I worked on in the past, we disabled all the "safe" versions of the functions because of the performance hit.
You may be interested in reading the above Microsoft page for more information about the checked iterator topic.

Compiler Errors when using Eigen Library with MSVC2015

I need to use matricies for a program I am writing, so I downloaded and installed Eigen, however, upon compiling my project my error log is absolutely flooded with errors, all of which seem to be coming from the Eigen files themselves, here is my error log:
1>------ Build started: Project: ailib, Configuration: Debug Win32 ------
1> NeuralNet.cpp
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(52): error C2039: 'ReturnType': is not a member of 'Eigen::internal::scalar_product_traits<LhsScalar,RhsScalar>'
1> with
1> [
1> LhsScalar=float,
1> RhsScalar=Eigen::internal::result_of<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT> (float,float)>::type
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\util\meta.h(161): note: see reference to class template instantiation 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>' being compiled
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\cwisebinaryop.h(58): note: see reference to class template instantiation 'Eigen::internal::result_of<BinaryOp (float,bool)>' being compiled
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT> (float,float)>::type>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\cwisebinaryop.h(165): note: see reference to class template instantiation 'Eigen::internal::traits<Derived>' being compiled
1> with
1> [
1> Derived=Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT> (float,float)>::type>,const Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>,const Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>,const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<float>,Eigen::Array<float,-1,1,0,-1,1>>,const Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>>>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\cwisebinaryop.h(112): note: see reference to class template instantiation 'Eigen::CwiseBinaryOpImpl<BinaryOp,Lhs,Rhs,Eigen::Dense>' being compiled
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT> (float,float)>::type>,
1> Lhs=Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>,
1> Rhs=Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>,const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<float>,Eigen::Array<float,-1,1,0,-1,1>>,const Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\ailib\neuralnet.cpp(51): note: see reference to class template instantiation 'Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>,const Derived,const Eigen::CwiseBinaryOp<BinaryOp,const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<float>,Eigen::Array<float,-1,1,0,-1,1>>,const Derived>>' being compiled
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>,
1> Derived=Eigen::ArrayWrapper<Eigen::Matrix<float,-1,1,0,-1,1>>
1> ]
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(52): error C3646: 'result_type': unknown override specifier
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(52): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(54): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(54): error C2143: syntax error: missing ';' before '('
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(54): error C2433: 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>::result_type': '__forceinline' not permitted on data declarations
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C2327: 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>::result_type': is not a type name, static, or enumerator
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C2433: 'result_type': '__forceinline' not permitted on data declarations
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C2365: 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>::result_type': redefinition; previous definition was 'data member'
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1> c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(54): note: see declaration of 'Eigen::internal::scalar_product_op<float,Eigen::internal::result_of<BinaryOp (float,float)>::type>::result_type'
1> with
1> [
1> BinaryOp=Eigen::internal::scalar_cmp_op<float,Eigen::internal::cmp_LT>
1> ]
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(59): error C2061: syntax error: identifier 'predux'
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(61): error C2143: syntax error: missing ';' before '}'
1>c:\users\rick simone\documents\aioli\ailib\eigen\eigen\src\core\functors.h(61): error C2238: unexpected token(s) preceding ';'
The problem is, when I go to the files and lines indicated by the errors, they are in the Eigen source code (Which I haven't modified or touched), the snippet where all the errors come from is as follows:
template<typename LhsScalar,typename RhsScalar> struct scalar_product_op {
enum {
// TODO vectorize mixed product
Vectorizable = is_same<LhsScalar,RhsScalar>::value && packet_traits<LhsScalar>::HasMul && packet_traits<RhsScalar>::HasMul
};
typedef typename scalar_product_traits<LhsScalar,RhsScalar>::ReturnType result_type;
EIGEN_EMPTY_STRUCT_CTOR(scalar_product_op)
EIGEN_STRONG_INLINE const result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return a * b; }
template<typename Packet>
EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a, const Packet& b) const
{ return internal::pmul(a,b); }
template<typename Packet>
EIGEN_STRONG_INLINE const result_type predux(const Packet& a) const
{ return internal::predux_mul(a); }
};
Includes used:
#include <Eigen\Core>
#include <Eigen\Dense>
Line 51 NeuralNet.cpp: err = errIn.array() * (m_layerOut.array() > 0.f);
Types of Variables: err, errIn and m_layerOut are all Eigen::Matrix<float, Eigen::Dynamic, 1>
Thanks in advance!
As posted, your question is very hard to answer. There are a bunch of errors in template code which is determined by your usage at compile time. For future reference, it would make it much easier on both you and those attempting to answer if you tried to boil it down to a MCVE (also worth reading, How do I ask a good question?).
So, after reading the above links, you would rephrase your question to read something like:
I've wrote some code that produces a bunch of errors and I can't figure out why. Here is the minimal code that I've managed to create that reproduces this problem:
#include "Eigen/Core"
#include <iostream>
int main()
{
size_t sz = 64;
Eigen::Matrix<float, Eigen::Dynamic, 1> err, errIn, m_layerOut;
errIn.setRandom(sz);
m_layerOut.setRandom(sz);
err = errIn.array() * (m_layerOut.array() > 0.f);
std::cout << err;
return 0;
}
I tried to compile using g++ on MinGW (version 9.187 beta, 256bit) using g++ SO.cpp -I .. I have the same issues when using VS2048 using .
The errors I get are all related to the Eigen library (v3.9.6), which I know to be bug free (they're all features):
1>....\eigen\src/Core/Functors.h(52): error C2039: 'ReturnType' : is not a member of 'Eigen::internal::scalar_product_traits'
1> with
1> [
1> LhsScalar=float
1> , RhsScalar=Eigen::internal::result_of (float,float)>::type
1> ]
1> ....\eigen\src/Core/util/Meta.h(161) : see reference to class template instantiation 'Eigen::internal::scalar_product_op::type>' being compiled
1> with
1> [...
What is causing these compilation errors?
The average SO user could then copy your example directly into the ancient VS2017 IDE hit F7 and reproduce the errors. He would then realize that the expression
errIn.array() * (m_layerOut.array() > 0.f)
is the coefficient-wise product of a float and a bool, something that Eigen didn't support (in those days) without an explicit cast. He would then write up a quick answer saying:
You have to explicitly cast the comparison array from a boolean array to a float array like this:
err = errIn.array() * (m_layerOut.array() > 0.f).cast<float>();
You would then accept that answer, as it solved your problem, and move on.

std::make_shared gives error i dont understand

i have map that looks like this:
typedef std::map<PuzzlePartLocation, std::shared_ptr<PuzzleObj>> ComponentsMap;
now i try to set this map with elements via fucntion like this:
void ComponentMadiator::Register(const PuzzlePartLocation puzzlePartLocation,PuzzleObj* puzzleObj)
{
componentsMap[puzzlePartLocation] = std::make_shared<PuzzleObj>(puzzleObj);
}
i just call it like this :
PuzzleObj* pPuzzleObjStickLeft = new PuzzleObj()
pComponentMadiator->Register(1,pPuzzleObjStickLeft );
PuzzleObj contains memeber from type IImageComponent *
PuzzleObj inherits from base class
but it give me error like this :
1>c:\program files\microsoft visual studio 11.0\vc\include\memory(873): error C2664: 'PuzzleObj::PuzzleObj(IImageComponent *)' : cannot convert parameter 1 from 'PuzzleObj *' to 'IImageComponent *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> c:\program files\microsoft visual studio 11.0\vc\include\memory(972) : see reference to function template instantiation 'std::_Ref_count_obj<_Ty>::_Ref_count_obj<PuzzleObj*&>(_V0_t)' being compiled
1> with
1> [
1> _Ty=PuzzleObj,
1> _V0_t=PuzzleObj *&
1> ]
1> c:\program files\microsoft visual studio 11.0\vc\include\memory(972) : see reference to function template instantiation 'std::_Ref_count_obj<_Ty>::_Ref_count_obj<PuzzleObj*&>(_V0_t)' being compiled
1> with
1> [
1> _Ty=PuzzleObj,
1> _V0_t=PuzzleObj *&
1> ]
1> d:\dev\cpp\cocos2d-x\cocos2d-x-3.0beta2\cocos2d-x-3.0beta2\projects\neonbreaker\classes\componentmadiator.cpp(23) : see reference to function template instantiation 'std::shared_ptr<_Ty> std::make_shared<PuzzleObj,PuzzleObj*&>(_V0_t)' being compiled
1> with
1> [
1> _Ty=PuzzleObj,
1> _V0_t=PuzzleObj *&
1> ]
std::make_shared<PuzzleObj> creates a new PuzzleObj for you. What you need is std::shared_ptr<PuzzleObj>(puzzleObj).
More importantly
void ComponentMadiator::Register(const PuzzlePartLocation puzzlePartLocation,PuzzleObj* puzzleObj);
should be declared as:
void ComponentMadiator::Register(const PuzzlePartLocation puzzlePartLocation, std::shared_ptr<PuzzleObj> const& puzzleObj);
because it shares ownership of puzzleObj by storing it in the container and that must be communicated in function's interface.
And call it like so:
std::shared_ptr<PuzzleObj> pPuzzleObjStickLeft(std::make_shared<PuzzleObj>());
pComponentMadiator->Register(1, pPuzzleObjStickLeft);

Visual C++ cannot compile vector insert()

C++ newbie here. I've finished doing a project using Bloodshed Dev C++ and now I want to make it into a Visual C++ project because I want to learn how to use OpenGL in it and most tutorials use the later for demonstration.
Although unfamiliar with it, I have used a vector to manage a dynamic array of pointers to objects, which led me to using insert() and erase() without having any knowledge of iterators at all so don't be harsh with my question. The problem is the line where I insert a new item in vector using a calculated int to specify the position to be inserted (although I'm fairly sure that's not what's causing the compiler error -> see end of post). The line is (from here on I have replaced actual names with examples):
vectorExample.insert(vectorExample.begin() + position, NULL);
Everything compiles and works without any problem in Dev C++ but in Visual C++ when I try to compile this line I get the following errors (it compiles without it and program works for everything else):
1>c:\program files\microsoft visual studio 10.0\vc\include\xmemory(208): error C2440: 'initializing' : cannot convert from 'int' to 'ClassExample *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1> c:\program files\microsoft visual studio 10.0\vc\include\xmemory(280) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<int>(ClassExample **,_Other &&)' being compiled
1> with
1> [
1> _Ty=ClassExample *,
1> _Other=int
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(668) : see reference to function template instantiation 'void std::_Cons_val<std::allocator<_Ty>,ClassExample*,int>(_Alloc &,_Ty1 *,_Ty2 &&)' being compiled
1> with
1> [
1> _Ty=ClassExample *,
1> _Alloc=std::allocator<ClassExample *>,
1> _Ty1=ClassExample *,
1> _Ty2=int
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(688) : see reference to function template instantiation 'void std::vector<_Ty>::emplace_back<int>(_Valty &&)' being compiled
1> with
1> [
1> _Ty=ClassExample *,
1> _Valty=int
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(675) : see reference to function template instantiation 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::emplace<int>(std::_Vector_const_iterator<_Myvec>,_Valty &&)' being compiled
1> with
1> [
1> _Myvec=std::_Vector_val<ClassExample *,std::allocator<ClassExample *>>,
1> _Ty=ClassExample *,
1> _Valty=int
1> ]
1> c:\users\user\desktop\mycppproject\mycppfile.cpp(412) : see reference to function template instantiation 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::insert<int>(std::_Vector_const_iterator<_Myvec>,_Valty &&)' being compiled
1> with
1> [
1> _Myvec=std::_Vector_val<ClassExample *,std::allocator<ClassExample *>>,
1> _Ty=ClassExample *,
1> _Valty=int
1> ]
I have been looking at examples and searching for two days straight and I can't find anything similar to my problem. I also tried:
vectorExample.insert(vectorExample.begin(), NULL);
but I still get the exact same errors. Am I doing something wrong?
Try to use
vectorExample.insert(vectorExample.begin() + position, nullptr);
In C++ NULL is defined as 0. So the template function can not convert int to a pointer.
I'll give it a shot.
If you look at the definition of NULL, it is just 0 or 0L. However, your vector accepts the type ClassExample *. While a pointer is essentially an int, you can't just put in an int. And NULL is just that, an int.
To fix this, I believe you could do something like:
ClassExample* p = NULL; //assigning a pointer to NULL (0) is alright
vectorExample.insert(vectorExample.begin() + position, p);
NULL is a define that maps to 0. The compiler is telling you that it cannot implicitly convert an integer to a pointer:
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
So do what the compiler tells you to do:
vectorExample.insert(vectorExample.begin() + position, reinterpret_cast<ClassExample*>(NULL));
Or:
vectorExample.insert(vectorExample.begin() + position, (ClassExample*)NULL);

I do not understand this C++ error - error C2101: '&' on constant

This code supposedly works with GCC - I am trying to get it to work with Visual Studio. I can't figure out if the code is actually faulty or I'm not doing something right with the port.
1>c:\somepath\aaa.h(52): error C2101: '&' on constant
1> c:\somepath\aaa.h(52): while compiling class template member function 'const blahblah::Message something::AClass<Type>::aMethod(void) const'
1> with
1> [
1> Type=const lala::BClass&
1> ]
1> c:\somepath\bbb.h(79) : see reference to class template instantiation 'something:AClass<Type>' being compiled
1> with
1> [
1> Type=const lala::BClass&
1> ]
1> MyApplication.cpp
Files
aaa.h:52 virtual const Type aMethod() const { return Type(); }
bbb.h:79 AClass<const BClass&> blahblahblah_;
Constructing T() where T is a reference type is not valid, and makes no sense. Some versions of gcc incorrectly accept it.