maybe you would know, I get an error:
error C2440: 'initializing' : cannot convert from 'int'
Conversion from integral type to pointer type requires reinterpret_cast
It goes to ' file in MS VS 2010 folder:
template<class _Other1,
class _Other2>
_Pair_base(_Other1&& _Val1, _Other2&& _Val2)
: first(_STD forward<_Other1>(_Val1)),
second(_STD forward<_Other2>(_Val2))
{ // construct from moved values
}
I was looking for different solutions but could not find a correct one.
The error says
'initializing' : cannot convert from 'int' to 'EnterFunctor *'
The only part of your code you share is
functors.push_back(make_pair(sessionStartFunc,
pair<EnterFunctor*, ExitFunctor*>(NULL,sessionStartExit)));
If NULL is #defined as 0 this gives you an int but you promised a pair of pointers, so as the next line of the error says you can use a cast to make NULL the right type of pointer.
Related
I have strange error when compiling open source lib
I using visual studio 2019
and I created a solution to the lib cmake -G "Visual Studio 16 2019" ../"
but I keep getting erros when compiling :
starting with this line :
CFUNC char *optarg = "";
C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(16,24): error C2440: 'initializing': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(16,20): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(30,27): error C2440: 'initializing': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(30,21): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(38,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(38,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(45,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(45,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(51,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(51,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(85,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(85,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(94,15): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(94,11): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
"" is a constant string. You can't just assign that to a non-constant char *, because then it could be mutated through the pointer.
According to the source code of libunistd, the implementation was taken from NetBSD, but a quick check¹ shows that NetBSD doesn't initialize optarg at all:
char *optarg; /* argument associated with option */
You could follow the same approach.
You could also turn off this error by passing /Zc:strictStrings- (note the hyphen) to your compiler. As long as the constant string isn't written to, there should be little harm in that.
You might want to report this as a bug to the library author.
¹ Not sure I'm looking at the latest version. It's from 2014.
It's not strange. The type of "" (or any string literal) is const char[] and the type of optarg is char*. So if the assignment was allowed you would be able to modify a string literal via the pointer optarg. Clearly that's not desireable.
In an older version of C++, this assignment was allowed as a special case, simply because there was too much old C code that did this. But this is no longer the case.
I have added sqlite3.c file into my project.
And #include. Here is the code:
#include <sqlite3.h>
using namespace std;
int main()
{
return 0;
}
I compile the program and it throws the following error:
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(15705): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(19741): error C2440: '=' : cannot convert from 'void *' to 'sqlite3_mutex *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(20665): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(20677): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(21142): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(21256): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(21411): error C2440: '=' : cannot convert from 'void *' to 'char *'
But there is no error that a header or file is not found. Everything is found and the errors are just outputted above
I have found the solution. I compiled the C code as C++, but changing it was not enough.
I write it for future visitors:
First, I had to change file's (ONLY FILE) property. Right-click on the file and select properties, under the C/C++, select Advanced and then select Compile As and set it to C (neither default nor C++).
Then, you should make sure that your .c file is compiled without clr. Well, to do that, under the same C/C++ set of menu, select "Common Langugae Runtime Support" and set it to No Support....
I'm using Visual Studio 17 and I would add to this, find the settings for "Precompiled Header" and set that to "Not Using Precompiled Headers".
I have MFC dialog based application.
void CThr_MfcDlg::OnBnClickedButton1()
{
this->SetWindowTextW(L"bla");
(CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello") ;
}
Line this->SetWindowTextW(L"bla"); changes form caption to bla
I expect line (CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello") ; should change caption to hello, but have compile error:
Error 1 error C2440: 'type cast' : cannot convert from 'void' to 'CThr_MfcDlg *'
Read this. Since -> operator's precedence (2) is higher than cast operator's (3), your code is parsed in this way:
(CThr_MfcDlg*) (GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello")) ;
To avoid this, you should use parenthesis with casting.
// this will be correct.
((CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG))->SetWindowText(L"hello");
I'm using Microsoft Visual C++ 6.0 and Microsoft Visual Studio 2008 to develop an academic computer vision project.
In this project i need to use OpenCV 1.1 (http://opencv.willowgarage.com/) and CvBlob (http://code.google.com/p/cvblob/).
I tried to compile this project with Microsoft Visual Studio 2008 and it compiles without errors.
With Visual C++ 6.0 i got a lot of errors.
OpenCV are not responsible of this behavior, because a trivial project with only OpenCV (without CvBlob) works well.
To understand the errors better I made an empty project with only the CvBlob inclusion.
I paste here a brief summary of the errors:
cvcontour.cpp(253) : error C2371: 'i' : redefinition; different basic types (and others similar to this. i solved with variable redefinition, every time)
cvcontour.cpp(318) : error C2664: 'thiscall std::vector<struct CvPoint,class std::allocator<struct CvPoint> >::std::vector<struct CvPoint,class std::allocator<struct CvPoint> >(unsigned int,const struct CvPoint &,const class std::allocator<struct CvPoint> &)' : cannot convert parameter 1 from 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' to 'unsigned int' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
cvtrack.cpp(278) : error C2440: 'initializing' : cannot convert from 'struct cvb::CvTrack *const ' to 'struct cvb::CvBlob *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Have you ideas on how can i solve these problems?
Thanks in advance for the help!
-------- UPDATE --------
I tried to edit and correct the code in order to elminate the three errors in my question.
The error C2664 seems to be the more difficult to cirmumvent...
I have replaced the indicted line
return new CvContourPolygon(dq.begin(), dq.end());
where CvContourPolygon is a typedef std::vector<CvPoint> CvContourPolygon;
with
deque<int>::iterator dq_it;dq_it = dq.begin();
CvContourPolygon v_tmp;
v_tmp.push_back(*dq_it);
while (dq_it != dq.end()){
v_tmp.push_back(*dq_it++);
}
First, what that i wrote is correct? Than, how can i solve the errors that occured from this?
Thank you in advance!
Errors (suppose that the first line is 318:
cvcontour.cpp(319) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' (or
there is no acceptable conversion)
cvcontour.cpp(321) : error C2664: 'push_back' : cannot convert parameter 1 from 'int' to 'const struct CvPoint &'
Reason: cannot convert from 'int' to 'const struct CvPoint'
No constructor could take the source type, or constructor overload resolution was ambiguous
cvcontour.cpp(322) : error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::deque<struct CvPoint,class std::allocator<struct CvPoint> >::iterator' (or there is no acceptable conversion)
cvcontour.cpp(322) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
-------- UPDATE2 --------
This code seems to work correctly!
deque<CvPoint>::iterator dq_it;
dq_it = dq.begin();
CvContourPolygon v_tmp;
for (dq_it = dq.begin(); dq_it != dq.end(); ++dq_it){
v_tmp.push_back(*dq_it);
}
//return new CvContourPolygon(dq.begin(), dq.end());
return &v_tmp;
C2371 - VC6 was sloppy with scope of local variables. Should be able to fix this by making the code use variable names unambiguously.
C2664 - looks like failure to initialize a vector using deque iterators - wrong overload on vector::vector() being called? Probably have to work around this by manually copying the deque elements to the new vector somehow.
C2440 - check the objects are compatible (VS2008 seems to think so) and add the appropriate cast.
EDIT:
Shouldn't your code look like this?
deque<CVPoint>::iterator dq_it;dq_it = dq.begin();
CvContourPolygon v_tmp;
for (dq_it = dq.begin(); dq_it != dq.end(); ++dq_it)
{
v_tmp.push_back(*dq_it);
}
I am getting the following error while migrating VC6 code to VS2008. This code works fine in VC6 but gives a compilation error in VC9. I know it is because of a compiler breaking change. What is the problem and how do I fix it?
error C2440: 'initializing' : cannot convert
from 'std::_Vector_iterator<_Ty,_Alloc>'
to 'STRUCT_MUX_NOTIFICATION *'
Code
MUX_NOTIFICATION_VECTOR::iterator MuxNotfnIterator;
for(
MuxNotfnIterator = m_MuxNotfnCache.m_MuxNotificationVector.begin();
MuxNotfnIterator != m_MuxNotfnCache.m_MuxNotificationVector.end();
MuxNotfnIterator ++
)
{
STRUCT_MUX_NOTIFICATION *pstMuxNotfn = MuxNotfnIterator; //Error 2440
}
If it worked before, I am guessing MUX_NOTIFICATION_VECTOR is a typedef
typedef std::vector<STRUCT_MUX_NOTIFICATION> MUX_NOTIFICATION_VECTOR;
The iterator for a container can often be mistaken with a pointer (because it works the same way) and, in the case of some stl implementations, it can actually be a pointer (it probably was the case with STL provided with VC6). But there is no guarantee about that.
What you should do is the following :
STRUCT_MUX_NOTIFICATION& reference = *MuxNotfnIterator;
// or
STRUCT_MUX_NOTIFICATION* pointer = &(*MuxNotfnIterator);
I think this should do the trick:
STRUCT_MUX_NOTIFICATION *pstMuxNotfn = &(*MuxNotfnIterator);
You'll need to dereference the iterator to get the appropriate struct (not sure why it worked before?):
STRUCT_MUX_NOTIFICATION *pstMuxNotfn = *MuxNotfnIterator;