Cannot convert from 'overloaded-function' to 'QLabel *'? - c++

So, I am trying to use the following code to pass an object to a function in Qt, then convert it to a QLabel for further processing (it's part of an animation sequence):
void myAnimation(QObject* label)
{
QLabel *lbl = qobject_cast<QLabel*>label;
//more code.....
}
Yet, whenever I try to compile, I get the following two errors:
error: C2440: 'initializing' : cannot convert from 'overloaded-function' to 'QLabel *'
Context does not allow for disambiguation of overloaded function
and
error: C2146: syntax error : missing ';' before identifier 'label'
Why isn't my code working? Any help is appreciated. Thank you!

Try:
QLabel *lbl = qobject_cast<QLabel*>(label);
// ^ ^

Related

Qt Throwing Illegal Error at QVariant

I have some code that is meant to get a QList of QMaps from a set of data. Then it should go through all the QMaps in that list. For some reason upon attempting this I am getting some errors regarding the QVector used to store data. Here is my code:
#include <QCoreApplication>
#include <QMap>
#include <QVariant>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QList<QMap<QString, QVariant>> maps;
QMap<QString, QVariant> item1;
item1.insert("Item 1", QVariant::fromValue(1));
maps.append(item1);
foreach (const QMap<QString, QVariant> & map, maps)
{
qDebug() << map;
}
return a.exec();
}
Error:
..\ErrorTest\main.cpp(17): warning C4002: too many actual parameters for macro 'Q_FOREACH'
..\ErrorTest\main.cpp(17): error C2275: 'QVariant': illegal use of this type as an expression
C:\Qt\5.8\msvc2015_64\include\QtCore/qsharedpointer_impl.h(94): note: see declaration of 'QVariant'
..\ErrorTest\main.cpp(17): error C2955: 'std::remove_reference': use of class template requires template argument list
D:\Microsoft Visual Studio 14.0\VC\INCLUDE\xtr1common(301): note: see declaration of 'std::remove_reference'
..\ErrorTest\main.cpp(17): error C2065: 'map': undeclared identifier
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ')' before '>'
..\ErrorTest\main.cpp(17): error C2059: syntax error: '>'
..\ErrorTest\main.cpp(17): error C2065: '_container_': undeclared identifier
..\ErrorTest\main.cpp(17): error C2228: left of '.control' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2228: left of '.i' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2228: left of '.e' must have class/struct/union
..\ErrorTest\main.cpp(17): note: type is 'unknown-type'
..\ErrorTest\main.cpp(17): error C2059: syntax error: ')'
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ';' before 'for'
..\ErrorTest\main.cpp(17): error C2059: syntax error: '='
..\ErrorTest\main.cpp(17): error C2143: syntax error: missing ';' before '}'
..\ErrorTest\main.cpp(17): fatal error C1004: unexpected end-of-file found
Thank you, and yes the tagData->toMappedList() returns the correct set of QMaps/data.
Imho the problem is the , inside the template. Sadly foreach is nothing but a call for Q_FOREACH which is a macro. It requires to arguments separated by comma. But because of the template you have 2 commas. I had this problem a while ago, though Qt Creator at least provided me with error: macro "Q_FOREACH" passed 3 arguments, but takes just 2 instead of the ton of errors you got. In your case this would be:
..\ErrorTest\main.cpp(17): warning C4002: too many actual parameters for macro 'Q_FOREACH'
I would suggest a for loop with an iterator to traverse the list of maps. Unless you want to dump the const in which case you can do:
QMap<QString, QVariant> map;
foreach (map, maps) {
...
}
You can also use auto but again - no constantness.
If constantness is a critical aspect in your case, go for the for loop with a constant iterator.

Void cast to type

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");

cannot convert parameter 1 from 'CStringListX *__w64 ' to 'CStringListX &'

I am making a wrapper for a Cpp DLL, but am getting trouble with the following function:
void PyGetUSBDeviceNames(DFUEngine *DFUe, CStringListX &devices)
{
return DFUe->GetUSBDeviceNames(&devices);
}
I get the error:
error C2664: 'DFUEngine::GetUSBDeviceNames' : cannot convert parameter 1 from 'CStringListX *__w64 ' to 'CStringListX &'
The function GetUSBDeviceNames is defined in the h file as:
static int GetUSBDeviceNames(CStringListX &devices);
how can I get around this one?
Thanks alot
Remove the unneeded &:
return DFUe->GetUSBDeviceNames(devices);

error C2440 and functors

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.

create multi-thread

I use Multi-thread method in vs2008 ,use c++ language. when I use _beginthreadex function, I got the follow error:
error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int
(__stdcall *)(void *)' to 'unsigned int (__stdcall *)(void *)'
ps: I use the template on the threadFunc like this:
template<class T>
unsigned int WINAPI closingReconstruction_ThreadFunc(void* pvPara)
{...}
,and then i creat the thread
_beginthreadex(NULL,0,closingReconstruction_ThreadFunc<T>,(PVOID)(tPara+i),0,NULL)
so i get this error , of course , if I remove the template ,it can be work,but i need to use template, are there any methods to solve this.
You forgot to mention the immediately preceding error message,
error C2065: 'T' : undeclared identifier
Let me just state that looking at the first error message first, is generally a good idea.
Cheers & hth.,