: error: ambiguous overload for ‘operator=’
-
I like to expand the use of my application and to this goal I like to
declare
QStringList expressionList ;
as class variable memberand define it in code
expressionList = {"([0-F]{2}[:]){5}[0-F]{2}","Agent registered","Controller","Pairable: yes"};I am receiving this error and I do not understand why and of course have no idea how to fix it.
If I define it this way , in body of the code , it works just fine as a "local variable".
QStringList expressionList = {"([0-F]{2}[:]){5}[0-F]{2}","Agent registered","Controller","Pairable: yes"};Do I have to build the list from individual QString ?
/mnt/MDI_RAID_5/MDI_WORK_COPY_FOLDER/MDI_BT_Aug1_BASE/MDI_BT/MDI_BT/C_BLUETOOTHCTL/C_bluetoothctl/mainwindow_bluetoothctl.cpp:5721: error: ambiguous overload for ‘operator=’ (operand types are ‘QStringList’ and ‘<brace-enclosed initializer list>’)
mainwindow_bluetoothctl.cpp: In member function ‘void MainWindow_Bluetoothctl::on_lineEdit_8_returnPressed()’:
mainwindow_bluetoothctl.cpp:5721:115: error: ambiguous overload for ‘operator=’ (operand types are ‘QStringList’ and ‘<brace-enclosed initializer list>’)
5721 | expressionList = {"([0-F]{2}[:]){5}[0-F]{2}","Agent registered","Controller","Pairable: yes"};
| ^ -
@AnneRanch hard to tell without more context, but I think this
expressionList = {"([0-F]{2}[:]){5}[0-F]{2}","Agent registered","Controller","Pairable: yes"}
is a probably a pure assignment, and not a construction operation and the compiler has a hard time deducting the type of the {}The easiest fix is probably being explicit:
expressionList = QStringList{"([0-F]{2}[:]){5}[0-F]{2}","Agent registered","Controller","Pairable: yes"}
-
@AnneRanch said in : error: ambiguous overload for ‘operator=’:
rror: ambiguous overload for ‘operator=’ (operand types are ‘QStringList’ and ‘<brace-enclosed initializer list>’)
You have run into a mean compiler feature.
In short: changing your assignment to
expressionList = QStringList({"([0-F]{2}[:]){5}[0-F]{2}","Agent registered","Controller","Pairable: yes"});
will solve your problem.The ambiguity is caused because the compiler could treat the curly-braces (i.e. a list of const char *) either as an initializer list or as iterators. A direct assignment is more permissive, that's why it works as local variable or with an assignment in the header. With the code above, a const QStringList object is directly assigned on the right side of the operator= and copied to the left side. That eliminates the ambiguity at the (relatively small) cost of creating another object.
I hazard the guess that everyone in this forum has stumbled over this issue more than once. And if there are any who haven't: It's because I took their turns ;-)
-
@Axel-Spoerl Thanks, appreciate all the help. Since I am actually after an array of these and some entries are duplicated I went little crazy and ended up building the list in code. I actually changed to pointers and it is doing what I need. If there is an interest I will post the result after I clean it up.
Thanks again