cannot convert parameter from 'PCHAR' to 'LPCWSTR' - windows

I have defined the pointer size_Drive as :
PCHAR size_Drive ;
then i used the function lstrlen :
size_Drive += (lstrlen(size_Drive) + 1) ; (line 28)
but it gives me the following ERROR :
1>c:\users\hp.hp-pc\documents\visual studio 2008\projects\getvolumeinfo\getvolumeinfo\getvolumeinfo.cpp(28) : error C2664: 'lstrlenW' : cannot convert parameter 1 from 'PCHAR' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

PCHAR is a typedef for char whereas LPCWSTR is a typedef for const wchar_t*, and in a Unicode build lstrlen is a macro for the Unicode function lstrlenW.
You should call lstrlenA specifically to use the ANSI function.

Related

Why an argument can be outside of ( ) in a function definition?

I have a piece of code in my .y file as follows
void yyerror(s) char *s; {
fputs(s, stderr);
putc('\n', stderr);
}
The function prototype declaration is in another file as follows
void yyerror (char const *s);
I tried to compile the code on both Linux and mac. On Linux, the compiler generates the following error message
error: argument ‘s’ doesn’t match prototype
void yyerror(s) char *s; {
^
On mac, the compiler generates the following warning message
warning: promoted type 'char *' of K&R function parameter is not compatible with the parameter type 'const char *' declared in a previous prototype [-Wknr-promoted-parameter]
void yyerror(s) char *s; {
^
My questions are:
Why clang only generates warning while cc generates an error.
Both compilers complain about 's' in "char *s". Why they ignore the 's' in () but consider the second 's' as the argument?

clang accepts "static_cast<const int&>" using "explicit operator int() const", gcc/VS reject

Given this c++11 code: (everything remains the same compiling as c++14 or c++17)
class typeSafe {
public:
typeSafe(int tValue)
: value(tValue) {
}
explicit operator int() const {
return value;
}
private:
int value;
};
int main() {
typeSafe x{5};
typeSafe y{5};
if(static_cast<const int&>(x) == static_cast<const int&>(y)) {
}
}
The controversy is because the static_cast attempts converting to a const int&, but the explicit operator gives an int, not an int&.
clang 5.0.0 silently compiles it, with every warning turned on.
gcc rejects it, with:
typeSafe.cpp: In function ‘int main()’:
typeSafe.cpp:17:32: error: invalid static_cast from type ‘typeSafe’ to type ‘const int&’
if(static_cast<const int&>(x) == static_cast<const int&>(y)) {
^
typeSafe.cpp:17:62: error: invalid static_cast from type ‘typeSafe’ to type ‘const int&’
if(static_cast<const int&>(x) == static_cast<const int&>(y)) {
^
Visual Studio 2017 15.5 Preview 2 rejects it, with:
typeSafe.cpp(17): error C2440: 'static_cast': cannot convert from 'typeSafe' to 'const int &'
typeSafe.cpp(17): note: Reason: cannot convert from 'typeSafe' to 'const int'
typeSafe.cpp(17): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Note if the static_cast attempts converting to an int&, then all 3 compilers reject it, with clang saying:
typeSafe.cpp:17:7: error: non-const lvalue reference to type 'int' cannot
bind to a value of unrelated type 'typeSafe'
if(static_cast<int&>(x) == static_cast<int&>(y)) {
^ ~
typeSafe.cpp:17:31: error: non-const lvalue reference to type 'int' cannot
bind to a value of unrelated type 'typeSafe'
if(static_cast<int&>(x) == static_cast<int&>(y)) {
^ ~
So, who's right and who's wrong?

differnt behaviour between clang and microsoft c++/QT5 with constructors and return values

According to microsoft visual studio 2013 (being built from QT creator, QT 5.4), this code is fine:
#include <string>
struct X {
X(std::string const &) {};
};
X wibble() { return ""; }
clang however says
test.cpp(53) : error: no viable conversion from 'const char [1]' to 'X'
X wibble() { return ""; }
test.cpp(49) : note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const char [1]' to 'const X &' for 1st argument
struct X {
test.cpp(49) : note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const char [1]' to 'X &&' for 1st argument
struct X {
test.cpp(50) : note: candidate constructor not viable: no known conversion from 'const char [1]' to 'const std::string &' (aka 'const basic_string<char, char_traits<char>, allocator<char> > &') for 1st argument
X(std::string const &) {};
It does the same thing with QString for what it's worth. Assuming clang is correct, why is it complaining?
It doesn't compile even in MinGW, because it is illegal. Because only one level of user-defined implicit conversion is legal. In your code you have 2 level conversion: to std::string and to X.
struct X {
X( std::string const &) {}
};
X wibble1() { return ""; } // 2 level: error
X wibble2() { return X(""); } // 1 level: ok
Even here you have same error:
void foobar(X x) {
}
int main()
{
foobar(X(""));// ok, 1 level
foobar ("f");
// error: could not convert '(const char*)"f"' from 'const char*' to 'X'
}

Error 4 error C2440: 'initializing' : cannot convert from 'void *' to 'char *'

I tried to compile this and got mistakes such as:
Error 4 error C2440: 'initializing' : cannot convert from 'void *' to 'char *'
This code is taken from here. Why doesn't it work?
DWORD dwRead=0;
char *lpData = (VOID*)GlobalAlloc(GMEM_FIXED, MAX_READ),
You appear to be compiling with a C++ compiler, so you'll need an explicit cast:
char *lpData = (char *)GlobalAlloc(GMEM_FIXED, MAX_READ);

MFC CString to int

using this code i get an error at atoi's conversion from CString to int:
int nrCentrala; CString idCentrala;
GetDlgItem(IDC_EDIT_IDCENTRALA)->GetWindowText(idCentrala);
nrCentrala = atoi(&idCentrala);
where IDC_EDIT_IDCENTRALA is an edit control.
Error 7 error C2664: 'atoi' : cannot convert parameter 1 from 'CString *' to 'const char *'
CString provides an overloaded operator for LPCTSTR. Also, use _tstoi instead, just in case you're compiling with Unicode libraries, e.g.
nrCentrala = _tstoi(idCentrala);
You pass pointer to CString (CString* type) instead of const char* which is expected by atoi(). Correct call is nrCentrala = atoi(idCentrala.GetString());

Resources