Error in “new_allocator”: cannot convert std::vector<double> to double in initialization

Hey everybody, I’m getting a strange error that doesn’t let me build my project. It’s the error in the title.


This is the full log. I have no clue what’s wrong here, although I do think the error comes from the use of many double vectors in the code. Any ideas what’s going on? Thank you for any help

Please, post your code. You are, probably, use extra parenthesis and passing vector type as an argument to the new operator.

Should be something like this:

auto pVec = new std::vector<double>();

and not something like that:

auto pVec = new(std::vector<double>);

5 Likes

That’s going to be problematic. I have about 20 header files, most of which are libraries (not platform dependent ones), all with hundreds of lines. Is there a way to narrow it down which file its coming from, as none show errors before compilation and on the log I can’t see any clear clue where the errors from (besides PROS files)

main.cpp.1261

6 Likes
std::vector<std::vector<double>> testInput2DVector;
std::vector<double> testInputVector;
std::vector<std::vector<double>> testOutput2DVector;

for (int i = 0; i > 100; i++) {
			testInputVector[0] = 1;
			testInputVector[1] = 0;

			testOutput2DVector[i][0] = .5f;
			//testInput2DVector[i].emplace_back(testInputVector); where the error
                                // was, though now its line 1330 because i did some editing of my code
 
		
		}

however i think the error log has changed even more. (mind you, i did comment out the line where i was passing a vector as a parameter). it was also the only occurence of emplace_back in my code.

********************************

Not sending analytics for this command.


Compiled src/main.cpp [ERRORS]

In file included from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\arm-none-eabi\thumb\v7-a+simd\softfp\bits\c++allocator.h:33,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\allocator.h:46,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\string:41,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\stdexcept:39,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\system_error:41,

                 from src/main.cpp:1:

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\ext\new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = double; _Args = {std::vector<double, std::allocator<double> >&}; _Tp = double]':

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\alloc_traits.h:512:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = double; _Args = {std::vector<double, std::allocator<double> >&}; _Tp = double; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<double>]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\vector.tcc:115:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::vector<double, std::allocator<double> >&}; _Tp = double; _Alloc = std::allocator<double>; std::vector<_Tp, _Alloc>::reference = double&]'

src/main.cpp:1330:53:   required from here

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\ext\new_allocator.h:156:4: error: cannot convert 'std::vector<double>' to 'double' in initialization

  156 |  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }

      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In file included from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\char_traits.h:39,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\string:40,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\stdexcept:39,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\system_error:41,

                 from src/main.cpp:1:

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h: In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const pros::Motor*; _OI = pros::Motor*]':

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:472:30:   required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = const pros::Motor*; _OI = pros::Motor*]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:506:42:   required from '_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = const pros::Motor*; _OI = pros::Motor*]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:514:31:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const pros::Motor*, std::vector<pros::Motor> >; _OI = __gnu_cxx::__normal_iterator<pros::Motor*, std::vector<pros::Motor> >]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:569:7:   required from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const pros::Motor*, std::vector<pros::Motor> >; _OI = __gnu_cxx::__normal_iterator<pros::Motor*, std::vector<pros::Motor> >]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\vector.tcc:238:31:   required from 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = pros::Motor; _Alloc = std::allocator<pros::Motor>]'

src/main.cpp:97:17:   required from here

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:380:18: error: use of deleted function 'pros::Motor& pros::Motor::operator=(const pros::Motor&)'

  380 |        *__result = *__first;

      |        ~~~~~~~~~~^~~~~~~~~~

In file included from ./include/api.h:73,

                 from ./include/main.h:37,

                 from src/main.cpp:13:

./include/pros/motors.hpp:27:7: note: 'pros::Motor& pros::Motor::operator=(const pros::Motor&)' is implicitly deleted because the default definition would be ill-formed:

   27 | class Motor {

      |       ^~~~~

./include/pros/motors.hpp:27:7: error: non-static const member 'const uint8_t pros::Motor::_port', cannot use default assignment operator

In file included from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\char_traits.h:39,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\string:40,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\stdexcept:39,

                 from c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\system_error:41,

                 from src/main.cpp:1:

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h: In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = pros::Motor*; _OI = pros::Motor*]':

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:472:30:   required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = pros::Motor*; _OI = pros::Motor*]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:506:42:   required from '_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = pros::Motor*; _OI = pros::Motor*]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:514:31:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = pros::Motor*; _OI = pros::Motor*]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:569:7:   required from '_OI std::copy(_II, _II, _OI) [with _II = pros::Motor*; _OI = pros::Motor*]'

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\vector.tcc:243:17:   required from 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = pros::Motor; _Alloc = std::allocator<pros::Motor>]'

src/main.cpp:97:17:   required from here

c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\stl_algobase.h:380:18: error: use of deleted function 'pros::Motor& pros::Motor::operator=(const pros::Motor&)'

  380 |        *__result = *__first;

      |        ~~~~~~~~~~^~~~~~~~~~

make: *** [common.mk:270: bin/main.cpp.o] Error 1

The error log still references emplace_back here: c:\users\manny\appdata\roaming\code\user\globalstorage\sigbots.pros\install\pros-toolchain-windows\usr\arm-none-eabi\include\c++\10.3.1\bits\vector.tcc:115:30: required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::vector<double, std::allocator<double> >&}; _Tp = double; _Alloc = std::allocator<double>; std::vector<_Tp, _Alloc>::reference = double&]'
You are misusing vector::emplace_back. A look at some documentation here shows that emplace_back takes an argument list and constructs the element in place. You should consider using push_back which takes an element.
Also

testInputVector[0] = 1;
testInputVector[1] = 0;
testOutput2DVector[i][0] = .5f;

will be trying to put elements into spots on the array that they are not allocated for. You should use vector::push_back until the arrays are the proper size, otherwise i believe you will get a memory permission error.

3 Likes

So i forgot what i didn(as it was a couple of days ago) but i fixed those errors. Now all thats left is some undefined reference errors, which ill figure out on my own since i know that error. Could i get the build code for terminal PROS though? Ive been looking for that command to build all files but i havent seen it in the forums

The build command is pros make. pros mu or pros make upload will build the code and upload it to the Brain. You can also open the Pros terminal (which receives data you print to the terminal using std::cout or printf) by pros t or use pros mut if you want to do all three.

3 Likes

Thanks! Luckily I quite literally just made a new project, copied and pasted my code, and then now it builds lol. Thanks though since I need to learn how to use the terminal

Ok, so even though it successfully compiled the first time, I went in again, compiled the same exact code, and it gave the same error again. Somethings off here. Could there be a reason why PROS is giving me undefined reference errors only after I close the editor and recompile?

Maybe somethings failing to load?

Edit: When I make a new project, the following appears:

Error: Command failed: pros c n "c:\Users\manny\OneDrive\Desktop\TestProject" v5 latest --machine-output --build-cache --no-sentry --no-analytics

Plus, main.h isnt found in the start of a project, and in api.h “In included file: main file cannot be included recursively when building a preamble”, which is just peachy. i fixed the first error on the project the code compiled, however i forgot how i did it. API i had no clue about even though it was giving an error.

I seriously dont understand how PROS has been so problematic for me, it took me 3 months to use it since it wouldnt download on any computer except my personal one, and now undefined references are just killing me here, even though im inheriting everything correctly.

Second edit: I made another new project, the c n error didn’t show up, it compiled, then I put my code in, compiled again, then I exited out of the editor, went back in, and then error. I’m convinced this is something with VSCode. This is the weirdest C++ thing I’ve encountered so far

Calling testInputVector[i] returns the underlying vector at index i. You cannot emplace a vector behind a vector, which is why there were an error. You’ll want to use push_back or reserve(1) + emplace_back on the 2D vector object in order to append the vector. Changing the code slightly seemed to compile and run fine for me.

Also, how you’re interacting with the vectors is also problematic. In the code, you used the [] operator to directly assign value to the vector. However, unlike arrays that have a set size defined, vectors are defaulted to be constructed with a size of zero. Therefore, within your code, index 0 does not exist, and calling it will result in an undefined behavior. (as shown in the photo below, nothing is printed due to an undefined behavior). You will want to either use push_back or reserve + emplace_back to increase the size of the vector.

Overall, it seems like most of the compile errors stemmed from misunderstandings of how vectors are used. I’ll strongly recommend you to watch some tutorials / read the api to have a better understanding on how vectors are used correctly.

4 Likes

Yeah, I’ll definitely go do some tutorials, as I wanted to review container classes anyways. do you have any idea about the “undefined reference” problem? It’s so strange that I just am at a loss for words