I have defined a createdirectory(const stdStr& path) in a class and I am accessing that function from another class using Directory::CreateDirectory("C:\\Temp");
I am getting an error on "C"\Temp" saying that "
no suitable constructor exists to convert from "const char [4]" to "std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>"
Because your "C:\\Temp" string is an array of char, but the function is using a string templated on wchar. Personally, I avoid Unicode like the plague, but I think you need L"C:\\Temp" (note the preceding L).
Related
I need to replace some %20 by spaces and got compile errors which i do not understand:
CString str = _T("foo%20bar");
str.Replace('%20',' '); // C4305 argument: truncation from 'int' to 'wchar_t'
str.Replace(_T('%20'),_T(' ')); // C4305 argument: truncation from 'int' to 'wchar_t'
str.Replace(_T("%20"),_T(" ")); // C2664 'int ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>::Replace(const wchar_t *,const wchar_t *)': cannot convert argument 1 from 'const char [4]' to 'wchar_t'
What is wrong?
The CString::Replace() method takes null-terminated string pointers as input, not individual characters. Your string literals need to use " instead of ', eg:
CString str = _T("foo%20bar");
str.Replace(_T("%20"), _T(" "));
Note that matches your last example, which you say is also erroring. The only way that can fail with the error message you have shown is if you have a misconfiguration in your project, where UNICODE is defined 1 but _UNICODE is not defined 2.
1: as evident by CString being mapped to CStringT<wchar_t>.
2: as evident by the compiler saying _T("%20") is a const char[] rather than a const wchar_t[].
CString uses TCHAR from the Win32 API, not _TCHAR from the C runtime library. Usually they are interchangeable, but not so in your situation. So, you need to either fix your project's configuration so that _UNICODE is also defined, or else use the Win32 TEXT() macro to match CString's use of TCHAR:
CString str = TEXT("foo%20bar");
str.Replace(TEXT("%20"), TEXT(" "));
Or, simply stop using TCHAR-based APIs altogether (TCHAR dates back to the days of Win9x/ME when Microsoft was pushing everyone to start migrating their code to Unicode), and really should not be used in modern coding if you can avoid it. Just use wchar_t strings directly instead, eg:
CStringW str = L"foo%20bar";
str.Replace(L"%20", L" ");
The last one should have worked, except that you seem to have a wide CString in a project without the UNICODE and/or _UNICODE macro defined.
In this combination, the _T() macro isn't giving you a compatible string literal. But L"whatever" will.
str.Replace(L"%20", L" ");
Notice that this does what you asked, but is not adequate for URL unescaping. You should convert all %xx sequences.
%20 may be formatted string like %d. and Replace function return replaced String and str is not replaced.
try like: str = str.Replace(_T("%%20"), _T(" "));
or
try like: str = str.Replace(_T("%20"),_T(" "));
Extra Info
If you look at this Format specification syntax: printf and wprintf functions article you will see the following clarification:
A basic conversion specification contains only the percent sign and a type character. For example, %s specifies a string conversion. To print a percent-sign character, use %%. ...
I'm writing my first Matlab script, and I get an error trying to use dir(). This is the script:
strLocation = "C:\Users\myname\Documents\MATLAB";
listing = dir(strLocation)
The error is:
Error using dir
Function is not defined for 'string' inputs.
What am I doing wrong?
It should first be noted that a char vector and a string are different things in Matlab. The string data type was introduced recently (in R2016b, I think). Previous versions do not support the string type, only char vectors.
Since the string data type was introduced, many built-in functions that used to accept char vector input can now accept string input as well. But this is being gradually incorporated into functions, apparently. So, even if your Matlab version supports the string data type, you may find some functions that still can only take a char vector as input. This seems to the case for dir in your version. In R2018b dir supports both types of input, according to the documentation.
So, you need to define the input to dir as a char vector. For this you use ' instead of ":
strLocation = 'C:\Users\myname\Documents\MATLAB';
listing = dir(strLocation)
Or, if you must have a string, convert it to a char vector before passing it to dir:
strLocation = "C:\Users\myname\Documents\MATLAB";
listing = dir(char(strLocation))
Since MATLAB R2017a double quotation marks denotes strings and single quotation marks denotes character vectors.
The dir function requires a char vector so you should call it with single quotation marks,
strLocation = 'C:\Users\myname\Documents\MATLAB';
listing = dir(strLocation)
I'm bothering you because I'm looking to convert a string into a float with node.JS.
I have a string that comes back from a request and looks like:
x,xxx.xxxx (ex. 4,530.1200) and I'd like to convert it to a float: xxxx.xxxx (or 4530.1200).
I've tried to use:
parseFloat(str) but it returns 4. I also found Float.valueOf(str) on the web but I get a ReferenceError: Float is not defined error.
You can use string replace to solve this kind of problem.
str = '4,530.1200';
parseFloat(str.replace(/,/g, ''));
This will remove all the , from your string so that you can convert it to the float using parseFloat.
Notice the g flag in regex, it represents global, by using this you are specifying to remove all the matched regex.
How can I access an individual character in Platform::String^?
I am using this variable type because it appears to be the only way to write a string to a TextBlock on a Universal Windows App.
I have tried the following methods to get individual characters to no avail:
String ^ str = "string";
std::string::iterator it = str->begin(); //Error: platform string has no member "begin"
std::string::iterator it = str.begin(); //Error: expression must have a class type
str[0] = 't' /*Error: expression must have a pointer-to-object or handle-to-C++/CX
mapping-array type*/
I am putting the String^ in a text block named "textBlock" as follows: textBlock->Text = str;
I am open to approaches other than modifying the Platform::String. My only requirement is that the string ends in a form that can be put into a TextBox
Platform::String represents a sequential collection of Unicode characters that is used to represent text. The controlled sequence is immutable: Once constructed, the contents of a Platform::String can no longer be modified.
If you need a modifiable string, the canonical solution is to use another string class, and convert to/from Platform::String when calling the Windows Runtime, or receiving string data. This is explained under Strings (C++/CX):
The Platform::String Class provides methods for several common string operations, but it's not designed to be a full-featured string class. In your C++ module, use standard C++ string types such as wstring for any significant text processing, and then convert the final result to Platform::String^ before you pass it to or from a public interface.
You could rewrite your code sample as follows:
// Use a standard C++ string as long as you need to modify it
std::wstring s = L"string";
s[0] = L't'; // Replace first character
// Convert to Platform::String^ when required
Platform::String^ ps = ref new Platform::String(s.c_str(), s.length());
An addition to the already supplied answer: you are also able to change a Platform::String^ into a std::wstring so you can modify it, then you can always change it back to Platform::String^
Platform::String^ str = "string";
wstring orig (str->Data());
orig[0] = L't';
str = ref new Platform::String(orig.c_str());
var trnlist = from tr in db.DebtorTransactions
join wr in db.Warranties
on tr.ProductID equals Convert.ToInt32(wr.PkfWarranty) into wrtd
from wr1 in db.Warranties
join sr in db.SalesReps
on wr1.fldSrId equals sr.pkfSrID into wrsr
from wr2 in db.Warranties
join ag in db.Agentsenter code here
on wr2.fldAgentID equals ag.pkfAgentID into wrag
select wrtd;
tr.ProductID is an int and wr.PKfWarranty is string.var rustul= convert.toint32(tr.ProductID) doesn't suitable for me.
Is there any built-in function of Linq to entity to do this?
Here you say:
tr.ProductID is a int
And then you try:
convert.toint32(tr.ProductID)
So... you're trying to convert an int to an int? In a comment you say:
the best overload method match for int.parse(string) has some invalid arguments
Well, if you're trying to call int.Parse() and passing it an int then you'd probably get that exact error. I imagine there's no overload for int.Parse() which accepts an int since, well, the value is already an int.
Let's look back at your problem description:
tr.ProductID is a int and wr.PKfWarranty is String
And you want to compare these two values? Then you'll either need to convert tr.ProductID to a string:
tr.ProductID.ToString()
or convert wr.PKfWarranty to an int:
int.Parse(wr.PKfWarranty)
A few things to note:
Converting from an int to a string is pretty safe, I doubt you'll ever have problems with that. However, converting from a string to an int assumes that the string can be converted to an int. This won't be the case if the string has anything in it that's not an int, or has a number too large to fit into the int data type. int.TryParse() exists for this purpose, but can be tricky to use in an in-line LINQ statement, especially when that statement is an expression tree which needs to produce SQL code.
If you convert the int to a string, there are different ways to compare strings. Depending on whether this is happening in resulting SQL code or in C# code makes a difference. If the latter, string.Equals() is the preferred method.