Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change only a specific Default Parameter on a function

Tags:

c++

qt

qt5

I'm using qt and I am trying to use the QInputDialog::getText() function to get input from the user, from the Documentation the definition of the function is:

QString QInputDialog::getText(QWidget * parent, const QString & title, const QString & label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(), bool * ok = 0, Qt::WindowFlags flags = 0, Qt::InputMethodHints inputMethodHints = Qt::ImhNone)

and here is my code:

bool ok=0;
newAddress = QInputDialog::getText(0,"Enter an Address to Validate",
                                       "Adress: comma separated (e.g Address line 1, Address line 2, City, Postal Code)"
                                       ,&ok);

but I get the Error:

error: no matching function for call to 'QInputDialog::getText(int, const char [29], const char [80], bool*)'
                                            ,&ok);
                                                ^

but when I pass in all the arguments before the *ok argument like:

bool ok=0;
newAddress = QInputDialog::getText(0,"Enter an Address to Validate",
                                       "Adress: comma separated (e.g Address line 1, Address line 2, City, Postal Code)"
                                       ,QLineEdit::Normal,
                                       QString(),&ok);

it works.

I really don't understand why cant I just change the default parameter I want and leave the rest as default?.

like image 794
Makhele Sabata Avatar asked Mar 06 '19 09:03

Makhele Sabata


3 Answers

When you pass a value for a particular parameter that has a default argument, you have to pass values for all the default parameters before it. Otherwise, the value you have passed will be taken as the value for the first default parameter.

So you have to do this:

newAddress = QInputDialog::getText(
             0,
             "Enter an Address to Validate",
             "Adress: comma separated (e.g Address line 1, Address line 2, City, Postal Code)", 
             QLineEdit::Normal, 
             QString(), 
             &ok);

You can leave out passing values for the parameters after bool * parameter.

The C++ standard states in [dcl.fct.default]/1

Default arguments will be used in calls where trailing arguments are missing.

like image 115
P.W Avatar answered Nov 20 '22 17:11

P.W


In C++ you can only use (one or multiple) default parameters at the end of the parameter list. If you omit parameters in the middle, the compiler has no way of knowing, which argument belongs to which parameter. Therefore you have to specify the default parameters QLineEdit::Normal and QString() manually before passing &ok.

In your not working case the compiler tries to match your bool pointer to the next type in the parameter list, which is QLineEdit::EchoMode and therefore not compatible.

like image 33
AlbertM Avatar answered Nov 20 '22 18:11

AlbertM


the error is beacuse of the optional paramteres:

QString QInputDialog::getText(
    QWidget * parent, 
    const QString & title, 
    const QString & label,
    QLineEdit::EchoMode mode = QLineEdit::Normal, 
    const QString& text = QString(), 
    bool * ok = 0,
    Qt::WindowFlags flags = 0, 
    Qt::InputMethodHints inputMethodHints = Qt::ImhNone)


QInputDialog::getText(
    0,
    "Enter an Address to Validate",
    "Adress: comma separated (e.g Address line 1, Address line 2, City, Postal Code)",
    --> QLineEdit::EchoMode ??  
    --> QString& text ??
    &ok);

if you set one optional parameter, you have to set all the optional paramteters to the left of that, in your case QLineEdit::EchoMode and QString& text

like image 3
ΦXocę 웃 Пepeúpa ツ Avatar answered Nov 20 '22 19:11

ΦXocę 웃 Пepeúpa ツ