How can I convert std::string to v8::string,when I am trying to pass an std::string to a script in the following way
script1 = Script::Compile(param1);
I am getting conversion error.
To convert a std::string to a v8::Local<v8::String>, you don't need to compile and run a script. Just do this:
std::string sText = "whatever";
v8::Local<v8::String> hTextJS = v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), sText.c_str());
Related
So I am having an issue using basename for one of my programming assignments for school
I have tried getting a simplier version of it working -- I got it working however, still confused exactly what I am supposed to do in this case
// $Id: util.cpp,v 1.1 2016-06-14 18:19:17-07 - - $
#include <libgen.h>
#include <cstring>
using namespace std;
#include "util.h"
ydc_exn::ydc_exn (const string& what): runtime_error (what) {
}
string exec::execname_; // Must be initialized from main().
int exec::status_ = EXIT_SUCCESS;
void exec::execname (const string& argv0) {
execname_ = basename (argv0.c_str());
cout << boolalpha;
cerr << boolalpha;
DEBUGF ('Y', "execname = " << execname_);
}
void exec::status (int new_status) {
new_status &= 0xFF;
if (status_ < new_status) status_ = new_status;
}
ostream& note() {
return cerr << exec::execname() << ": ";
}
ostream& error() {
exec::status (EXIT_FAILURE);
return note();
}
Just trying to get the code to compile --- my error message in c++ is:
'
util.cpp:15:16: error: no matching function for call to 'basename'
execname_ = basename (argv0.c_str());
^~~~~~~~
/usr/include/libgen.h:40:7: note: candidate function not viable: 1st argument ('const std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>
>::value_type *' (aka 'const char *')) would lose const qualifier
char *basename(char *);
^
1 error generated.
make: *** [util.o] Error 1'
basename takes char* as argument, which is not const, meaning the function is allowed to modify the value.
const string& argv0 is const, meaning the value of argv0 must not be modified.
Thus you are not allowed to call basename with argv0.c_str() as parameter, as that would violate the const qualifier.
The error message is clear: 1st argument ... would lose const qualifier.
So either change argv0 to not be const (probably not a good idea), or change basename to take a const char* parameter (probably the better idea), or change basename to work with std::string instead of char* like the rest of your code (probably the best idea).
So apparently basename is a unix function that you cannot change. (Thanks Nevin!) From the manpage:
Both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions.
In that case I recommend creating a copy of argv0. The simplest way of doing this would be to change the signature of execname to this:
void exec::execname(std::string argv0)
I think that your central problem is including libgen.h as well as cstring. If you look at the prototypes for basename in those files, they're different. If you're building this on linux, you'll see a comment in the libgen.h version which should be enlightening.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/basename.html and
https://linux.die.net/man/3/basename are useful here too.
I suggest removing the #include <libgen.h> and trying again.
Unable to get basename to work in C++, I am trying to just get the code to compile.
This is for a school assignment and I have tried to see if it is my stdlib having issues -- however, it seems that the problem is that
'''c++
#include<iostream>
#include<cstring>
#include<libgen.h>
int main(){
const char *ch = "asdfasdf/asdf.cpp";
std::cout<<basename(ch) << std::endl;
std::cout<<dirname(strdup(ch)) << std::endl;
return 0;
}
I was supposed to print out the output of ch, however, instead I get the error message:
test.cpp:8:14: error: no matching function for call to 'basename'
std::cout<enter code here
/usr/include/libgen.h:40:7: note: candidate function not viable: 1st argument ('const char *') would lose const qualifier
char *basename(char *);
^
1 error generated.
basename's argument is char*, so this function may modify passed data. You cannot pass const char* - pointer to only-read data into basename because it would let basename modify const data - not allowed.
Create your input as modifiable array:
char ch[] = "asdfasdf/asdf.cpp";
ALL,
Can someone explain to me why this code:
std::wstring query1 = L"SELECT....";
res = mysql_query( m_db, m_pimpl->m_myconv.from_bytes( query1.c_str() ).c_str() );
gives me an error from the subject?
I do have -DUNICODE defined inside C++ options
I guess I just need a pair of fresh eyes.
Thank you.
It is on Gentoo Linux with gcc5.4.
This is a way to convert a unicode wide-character string to a const char*
char query_cstr[100];
size_t charsConverted;
wchar_t* unicode_query = L"SELECT * FROM table;";
wcstombs_s(&charsConverted, query_cstr, unicode_query, wcslen(unicode_query));
const char* query_const = query_cstr;
//Use query_const inside of mysql_query now that it's been converted to a const char*
I've run into trouble using the locale functions for various reasons. wcstombs_s() makes things a bit easier when converting unicode. Using c_str() on a std::wstring object will yield a const wchar_t* string, which is not what you want.
I want to convert to std::string to System::String^ in Visual C++ environment. I know that we can convert System::String to std::string by the MarshalString Function as below:
void MarshalString ( String ^ s, string& os ) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
I can't find the way to convert std::string to System::String but I found that System::String has constructor with argument as below :
System::String(Char* value, Int32 startIndex, Int32 length)
and i try to use code like below, but it can not give me a correct solution :
std::string str1 = "MyString";
System::String^ str = new System::String(str1.c_str(), 0, str1.length());
What wrong happen in my code?
Microsoft provide their C++ Suppport Library with Visual Studio to facilitate interaction between C++ and C++/CLI. That library provides the template function marshal_as which will convert a std::string to a System::String^ for you:
#include <msclr\marshal_cppstd.h>
std::string stdString;
System::String^ systemString = msclr::interop::marshal_as<System::String^>(stdString);
I need to send multibyte char to a socket after appending '\n' but what I have is a const wchar_t*. How can I convert it?
If your question is how to actually manipulate the contents of a constant, then consider const_cast.
Appending an '\n' to a const wchar_t* string means you have to make a copy of the original string. Read this MS docs on how to use swprintf for that:
http://msdn.microsoft.com/en-us/library/ybk95axf%28VS.71%29.aspx
If your problem is the conversion, the WideCharToMultiByte Function is your friend:
http://msdn.microsoft.com/en-us/library/dd374130%28VS.85%29.aspx
You don't need to use direct newline characters to do this:
const wchar_t* original(L"original value");
std::wostringstream streamVal;
streamVal << original << std::endl;
const std::wstring modified(streamVal.str());
Going through a _bstr_t seems a pain but allows you to do the wide char to multibyte conversion pretty easily (small code). Include comsuppw.lib in the list of libraries in your project.
#include "comutil.h"
_bstr_t bstrVal(modified.c_str());
const char* multibytes((const char*)bstrVal);
std::cout << multibytes; // includes newline
How can I convert const wchar_t* to wchar_t*?
reinterpret_cast<const wchar_t*>(L"Test");
Should really work.