Visual C++ cannot compile vector insert() - c++

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);

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.

Failure when casting NULL/nullptr to std::function<>&

This code fails to build in VC2013: (EDIT: I'm not asking why it fails to build)
#include <functional>
struct MyStruct
{
std::function<void()> m_Func;
MyStruct( const std::function<void()>& func) : m_Func(func) {}
};
int main()
{
MyStruct rc( NULL );
return 0;
}
With error:
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xrefwrap(283): error C2064: term does not evaluate to a function taking 0 arguments
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<int,false>::_ApplyX<_Rx,>(void)' being compiled
1> with
1> [
1> _Ret=void
1> , _Rx=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<int,false>::_ApplyX<_Rx,>(void)' being compiled
1> with
1> [
1> _Ret=void
1> , _Rx=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(226) : while compiling class template member function 'void std::_Func_impl<_MyWrapper,_Alloc,_Ret,>::_Do_call(void)'
1> with
1> [
1> _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Ret=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(495) : see reference to class template instantiation 'std::_Func_impl<_MyWrapper,_Alloc,_Ret,>' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Ret=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Fty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Fty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,>>>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Fty=int
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,>>>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Fty=int
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<_Ty>(_Fty &&)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Fty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<_Ty>(_Fty &&)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Fty=int
1> ]
1> f:\work\teststdfunction\teststdfunction.cpp(16) : see reference to function template instantiation 'std::function<void (void)>::function<int>(_Fx &&)' being compiled
1> with
1> [
1> _Fx=int
1> ]
1> f:\work\teststdfunction\teststdfunction.cpp(16) : see reference to function template instantiation 'std::function<void (void)>::function<int>(_Fx &&)' being compiled
1> with
1> [
1> _Fx=int
1> ]
(Note the '_Fx=int' at the last two reported errors).
I can live with that, as changing MyStruct rc(NULL) to MyStruct rc(nullptr) solves the error. Two things however remain a mystery:
1) Removing the const qualifier from MyStruct ctor (MyStruct( std::function<void()>& func) gives a very, very different error:
1>f:\work\main\dev\common\teststdfunction\teststdfunction\teststdfunction.cpp(16):
error C2664: 'MyStruct::MyStruct(const MyStruct &)' : cannot convert
argument 1 from 'int' to 'std::function &'
Which makes more sense than the original error, and now fixing NULL to nullptr doesn't solve it. Why does int (or nullptr) refuse to cast to std::function<>& but agree to cast to a const std::function<>& ?
2) The original code compiles and works as expected in VS2010. Is it an obscure VS2010 library bug?
EDIT:
As far as the const/non const question goes, I now think the casting involved and potential type mismatch are probably a red herring. The argument passed - either NULL or nullptr - is a literal and thus a const. It just cannot bind to a non const reference. For example:
const int& a = 8; // Ok
int& b = 9; // error C2440: 'initializing' : cannot convert from 'int' to 'int &'
Does that sound right? Am I still missing something?
The special here has to do with constructor template constraints and other implicit conversions.
The reason why nullptr works is because std::function has a particular constructor taking it. This constructor will always be the best match for that argument because function templates are ranked as lower preference, all else being equal.
Normally, 0 will implicitly convert to nullptr and that's fine. The problem is that it can also be passed to the unconstrained function template constructor which constructs from function objects. This does not require an implicit conversion, so all is not equal, so this constructor is preferred- which results in the error that you see that int is not a valid function object.
libstdc++ and libc++ don't not exhibit this behaviour because they have implemented the C++14 fix for this problem, which constrains the constructor. C++11 did not so this behaviour is quite conforming for a C++11 implementation.
This kind of issue is why NULL is a terrible thing that you shouldn't use. In fact, the VS2010 team had to rush nullptr out the door as an additional feature in the last minute because NULL interacts so badly with every other feature in C++, ever, and especially with C++11.
For const vs non-const references, other answers have explained that issue adequately.
There are other WTFs you can find when using std::function without the constrained constructor fix shipped in C++14- this isn't the only one. The long and short is that it's a defect in the C++11 Standard, and not in VS. VS2010 compiling it is probably a compiler overload resolution bug.
Casting a T to a U& never works, and that's intentionally. You can cast to a U const&. The reason is that changes to the temporary U object wouldn't propagate back to the T value.
VS2010 is slightly buggy in this respect, and did allow the cast (but with the proper settings, will warn about it)

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);

Boost fast pool allocator has problems with function pointers?

Is this a Boost bug or am I doing something wrong?
#include <map>
#include <boost/pool/pool_alloc.hpp>
int main()
{
typedef const std::string key;
typedef double* (*value)(const int&);
std::map<key, value, std::less<key>> map_with_standard_allocator; // works
std::map<key, value, std::less<key>, boost::fast_pool_allocator<std::pair<const key, value> > > map_with_boost_allocator; // fails
}
the last line fails to compile under MS Visual Studio 2008 with Boost 1.40 and 1.48. It compiles fine under g++ 4.5.3 (Cygwin), though.
The error is:
1>Compiling...
1>main.cpp
1>C:\UniLib1\trunk\External\boost/pool/pool_alloc.hpp(205) : error C2535: 'const std::basic_string<_Elem,_Traits,_Ax> *boost::fast_pool_allocator<T,UserAllocator,Mutex,NextSize>::address(const std::basic_string<_Elem,_Traits,_Ax> &)' : member function already defined or declared
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>,
1> T=const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1> UserAllocator=boost::default_user_allocator_new_delete,
1> Mutex=boost::details::pool::default_mutex,
1> NextSize=32
1> ]
1> C:\UniLib1\trunk\External\boost/pool/pool_alloc.hpp(202) : see declaration of 'boost::fast_pool_allocator<T,UserAllocator,Mutex,NextSize>::address'
1> with
1> [
1> T=const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1> UserAllocator=boost::default_user_allocator_new_delete,
1> Mutex=boost::details::pool::default_mutex,
1> NextSize=32
1> ]
1> c:\Program Files\Microsoft Visual Studio 9.0\VC\include\xtree(137) : see reference to class template instantiation 'boost::fast_pool_allocator<T,UserAllocator,Mutex,NextSize>' being compiled
1> with
1> [
1> T=const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1> UserAllocator=boost::default_user_allocator_new_delete,
1> Mutex=boost::details::pool::default_mutex,
1> NextSize=32
1> ]
1> c:\Program Files\Microsoft Visual Studio 9.0\VC\include\map(78) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1> with
1> [
1> _Traits=std::_Tmap_traits<key,value ,std::less<key>,boost::fast_pool_allocator<std::pair<key,value >>,false>
1> ]
1> .\main.cpp(9) : see reference to class template instantiation 'std::map<_Kty,_Ty,_Pr,_Alloc>' being compiled
1> with
1> [
1> _Kty=key,
1> _Ty=value,
1> _Pr=std::less<key>,
1> _Alloc=boost::fast_pool_allocator<std::pair<key,value >>
1> ]
This is not a bug in VS2008 (as I mistakenly claimed in an earlier edit to this answer). The C++03 standard requires that the key type for an associative container, like std::map, must be 'assignable' (per Table 69 in 23.1.2 "Associative containers"). And a const std::string is not assignable. Note that the C++11 standard seems to relax this requirement, but the new standard doesn't apply to since VC++ 2008.
It's not clear to me that a compiler is required to diagnose code that tries to use std::map with a non-assignable key, so I don't think one can claim that GCC or VC++ 2010 are accepting this code improperly (I think it falls into the area of undefined code that works as you might expect, even though there's no guarantee it'll work). However it is clear that it's OK for VC++ 2008 to refuse to compile it.
All that said, I think that VC++ 2008's library parameterizing the allocator's address() function on the map's key rather than map's element is still suspect (see the first edit of this answer for details if you're interested), but I don't think there's any real bug there since the std::pair<> used to hold the map element will always be set up such that key part will be at the same address as the whole element.

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.