I need to make and WCHAR.
But it wont work, and i always get an error:
Error C2440 'initializing': cannot convert from 'const wchar_t [11]' to 'WCHAR *'
StateError (active) E0144 a value of type "const wchar_t *" cannot be used to initialize an entity of type "WCHAR *
My code:
WCHAR *Testlooll = L"TEST";
L"TEST" is a string literal of type const wchar_t[5], which is an array of const characters (since the literal exists in read-only memory). You are trying to initialize a WCHAR*, which is a pointer to a non-const character, to point at that array.
Initializing a pointer to non-const character data to point at an array of const character data is deprecated in C++98 (to maintain backwards compatibility with legacy code), and is illegal in C++11 onwards.
You need to change the declaration of Testlooll according:
const WCHAR *Testlooll = L"TEST";
Or:
LPCWSTR Testlooll = L"TEST";
In addition to Remy Lebeau's answer, if for some reason you can't modify the defination of Testlooll. You can just cast the const arry to a wchar_t*. For example,
struct someLibaryType
{
WCHAR *Testlooll
};
someLibaryType a;
a.Testlooll = (wchar_t*)L"TEST";
Someone maybe argue should cast to WCHAR* just keep same with the defination type of Testlooll. But in this context, you've already used L to identify a string, so it has to be wchar_t*.
Related
first of all this is not duplicate one because same error Question
asked before but in different context
1
code
#include<iostream>
#include<cstring>
int main()
{
const char *s1;
const char *s2="bonapart";
s1=new char[strlen(s2)+1];
strcpy(s1,s2);
std::cout<<s1;
}
Output
[Error] invalid conversion from 'const char*' to 'char*' [-fpermissive]
Why such error ?
If I replace const char *s1 by char *s1 compile fine. But I think this const only saying that s1 is pointing to constant string means we can't modify that string whom it is pointing. It does not mean that s1 is constant ptr. please put some light on this.
Why such error ?
Look at the declaration of strcpy:
char* strcpy( char* dest, const char* src );
Pay particular attention to the first parameter. And very particular attention to the type of the first parameter: char*. It isn't const char*.
Now, look at the type of the argument that you pass.
const char *s1;
It's not char*. It's const char*. You cannot pass a const char* into a function that accepts char* because former is not convertible to the latter, as the diagnostic message explains.
But I think this const only saying that s1 is pointing to constant string means we can't modify that string whom it is pointing.
That's exactly what const bchar* means. So, how do you expect strcpy to modify the content of the pointed string when you know that it cannot be modified? Technically in this case the pointed string isn't const, it's just pointed by a pointer-to-const
As you say, const char *s1; means that the string pointed at by s1 is not modifyable.
On the other hand, strcpy(s1,s2); will modify the string pointed at by s1.
This is against the condition "the string pointed at by s1 is not modifyable".
This question already has answers here:
Why is conversion from string constant to 'char*' valid in C but invalid in C++
(4 answers)
Closed 3 years ago.
very simple code does warn me. Some hints are not constructive. Warning is:
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
I tried:
char const *q = "pin";
char const *r = "\n\r";
{
while(client.findUntil(*q, *r))
without success
Origin code:
while(client.findUntil("pin", "\n\r"))
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
while(client.findUntil("pin", "\n\r"))
The warning means that your program is ill-formed. You didn't show the declaration, but from context, we can deduce that the argument of findUntil is char*. You may not pass a string literal to such function.
It used to be well-formed - but deprecated - to pass a string literal as char* prior to C++11.
I tried:
char const *q = "pin";
char const *r = "\n\r";
These are correct by themselves, but
while(client.findUntil(*q, *r))
This makes no sense. Previously your were attempting to pass a string, but now you indirect through the character pointer so you are passing a character. Unless the function is a template, this cannot possibly work.
findUntil(q, r) won't work either, because the pointers to const won't implicitly convert to pointers to non-const.
A correct solution is to copy the string literals into modifiable arrays:
char q[] = "pin";
char r[] = "\n\r";
{
while(client.findUntil(q, r))
Another is to fix findUntil to accept a pointer to const char instead. Then you can use string literals, since they can be converted to a pointer to const char.
I am trying to create a TCHAR* variable by using
TCHAR* example = TEXT("example");
but it wont even compile and says: A value of type const wchar_t* cannot be used to initialize an entity of type TCHAR*.
What should I do?
You have to add const, because the TEXT() macro returns a pointer to a const wchar_t.
const TCHAR* example = TEXT("example");
If the assignment were allowed without the const, you would be able to modify the const wchar_t data through the pointer.
See also A value of type "const char*" cannot be used to initialize an entity of type "char *"
Definition of method is:
void setArgument(char *);
And i call that method with this code:
setArgument("argument");
But my VisualStudio compiler gets me the next error:
cannot convert argument 1 from 'const char [10]' to 'char *'
Is it possible to send arguments like this or I must change arguments type in the method?
Also, VS show me next note in output: note: Conversion from string literal loses const qualifier (see /Zc:strictStrings)
The problem is that string literals are arrays of constant characters.
While the array could easily decay to a pointer to its first element, the type of that pointer is const char *. Which needs to be the type of the argument for your functions.
And if you need to modify the string you pass, then you should create your own non-constant array:
char argument[] = "argument";
setArgument(argument);
Of course, since you're programming in C++ you should stop using char pointers and arrays and instead use std::string.
It's possible, just if you really need the argument to be mutable (char* and not char const*), you need to allocate a new storage in the mutable memory and clone the contents of the constant memory to there, if that fits into your definition of "convert".
auto const len = strlen(input);
auto const buf = std::unique_ptr<char[]>(new char[len + 1]);
memcpy(buf, input, len + 1);
If you actually need char const* and if you are C++17 or later, you can possibly change the signature to setArgument(std::string_view arg), making it misuse-proof.
I have the following method declaration:
const String& MyClass::GetAspect(const String& strKey) const
In this method, we've decided to do a null-pointer check before doing some stuff; if the pointer inside this method is null, we want to just return null.
However, I get the following error:
myclass.cpp(222) : error C2440: 'return' : cannot convert from 'int' to 'const String &'
Reason: cannot convert from 'int' to 'const String'
No constructor could take the source type, or constructor overload resolution was ambiguous
Could someone help me understand this? Is there some const-correctness concept I don't fully understand here?
NULL is not an object of type const String, so of course you can't return it when a reference to const String is expected. In fact, one of the major advantages of references is that they can't (ever) be NULL.
Re-define the function to return const String *, or return an empty String.
NULL is a pointer (or technically, the integer value zero, which can be converted to/from a pointer, nullptr is a pointer with the value zero).
A std::string& is not a pointer (or an integer), so you can't use NULL for it. You could return "" or "Unknown" or something like that.