0
votes

I use Visual Studio 2013 and I get the following error:

error C2664: 'DWORD Options(int,LPCTSTR *,LPCTSTR,...)' : cannot convert argument 2 from 'LPTSTR []' to 'LPCTSTR *' 54 1 ConsoleApplication3

This is the code:

DWORD Options(int argc, LPCTSTR argv[], LPCTSTR OptStr, ...){
    // Code
}
int _tmain(int argc, LPTSTR argv[]){
   iFirstFile = Options(argc, argv, _T("s"), &dashS, NULL);
   // Code 
}

Does anyone know how to fix it?
And explain why this error does occur?

1
use Options(argc, const_cast<LPCTSTR*>(argv), _T("s"), &dashS, NULL); - RbMm
@RbMm: Why would you ever use a const_cast to add a const qualifier? - IInspectable
RbMm it no working. It error: Error 2 error LNK2019: unresolved external symbol __imp__ReportError referenced in function _main and error LNK1120: 1 unresolved externals - zzZOsiroZzz
Do you understand the error message? - David Heffernan
@zzZOsiroZzz - how this related to const_cast<LPCTSTR*>(argv) ? - RbMm

1 Answers

2
votes

"And explain why this error does occur?"

The reason behind this error can be found here: an implicit conversion "... would let you silently and accidentally modify a const object without a cast..."

"Does anyone know how to fix it?"

LPCTSTR argv[] is not a constant object, but an array of constant strings. The array itself may be modified (argv[0] = 0;). Since the advice in the link above is to avoid casting ("...please do not pointer-cast your way around that compile-time error message..."), the simplest solution is to change the signature of Options (notice the added const):

DWORD Options(int argc, const LPCTSTR argv[], LPCTSTR OptStr, ...)