I am doing some testing in Vexcode and I can’t get the std::to_string to work. I have imported and string.h and VC is returning the error “no member named ‘to_string’ in namespace ‘std’”. Any help would be appreciated!
1069B - Argonauts
You need to use parentheses and a parameter for the to_string method.
std::to_string(num);
Ya I’m doing more research and I think it has to do with the gcc VCS and VC use. Ya give me a minute
#include <iostream>
#include "stdarg.h"
#include <cstring>
#include <string.h>
#include "vex.h"
using namespace vex;
vex::brain Brain;
int varA = 42;
int main()
{
std::string sum = std::to_string(varA);
}
There is the a simple test code
Try including #include <string>
. It is possible the .h
is a legacy header or something.
I tried that and also removed the #include <string.h> in the vex.h file and am still getting a build error. Is any one else having this issue?
I’m on mac 10.14.5 and VC 19.04.1216 with SDK 20190410_18_00_00
std::to_string is one of the C11 methods that’s not always available in every standard library, google and you will find many others having issues using it. Here is a workaround if you really want to use that type pf functionality.
#include "vex.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace vex;
// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain Brain;
// define your global instances of motors and other devices here
int varA = 42;
template <typename T>
std::string to_string(T value)
{
std::ostringstream os ;
os << value ;
return os.str() ;
}
void
test_string() {
std::string s = "String";
s.append(" works");
std::cout << s << std::endl;
s = to_string( varA );
std::cout << s << std::endl;
}
int main() {
test_string();
while(1) {
// Allow other tasks to run
this_thread::sleep_for(10);
}
}
Thank you, can you give a technical reason why VCS/VC doesn’t have this function? Also is there a way to include other methods into the library so a workaround isn’t needed?
I don’t have a definitive answer, and don’t really have time to dig in and figure out exactly why, but the most likely reason is that the C++ standard library we include with VEXcode is a couple of years old and just may not include that function. At some stage we may update, but that’s more involved than just updating the library and headers, part of the C++ standard library we use is built into vexos, it’s why VCS/VEXcode programs are usually so much smaller (the final binary file that is sent to the V5) than say a PROS program.
Okay, that makes a lot of sense, thank you!