0

I had a problem with passing a template iterator of vector<'T> to a function QString::fromStdString(const std::string&).

I had my function that in case of type T can convert a std::string to QString or int to QString and than add in into my QTableWidget cell.

The issue happen with converting from std::string with:

error: no matching function for call to ‘QString::fromStdString(int&)’ QString text = QString::fromStdString(*it);

I can't understand why type of iterator object has int&. What I do wrong? Can I convert it in other way?

template<typename T>
void Dialog::Browse(vector<T> *list, int &counter, QTableWidget *table,     int column)
{
    QTableWidgetItem* item = 0;
    typename vector<T>::iterator it;
    for (it = list->begin(); it != list->end(); ++it){
        QString text;
        if (typeid(*list) == typeid(vector<string>)){
            //QString text = QString::fromUtf8(static_cast<string>(it)->c_str());
            QString text = QString::fromStdString(*it);
        } else if (typeid(*list) == typeid(vector<int>)){
            QString text = QString::number(*it);
        }

        item = new QTableWidgetItem(text);
        item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        table->setItem(counter, column, item);
        counter++;
    }
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402

1 Answers1

0

The problem here is that if statements don't stop the code from being compiled. When you do

if (typeid(*list) == typeid(vector<string>)){
    //QString text = QString::fromUtf8(static_cast<string>(it)->c_str());
    QString text = QString::fromStdString(*it);
} else if (typeid(*list) == typeid(vector<int>)){
    QString text = QString::number(*it);
}

The line QString text = QString::fromStdString(*it); will be compiled no matter if list is a vector<string> or not. Since when you called it with a vector<int> it tries to compile that code using a int& and you get your error.

If you have C++17 you can use a if constexpr like

if constexpr (typeid(*list) == typeid(vector<string>)){
    //QString text = QString::fromUtf8(static_cast<string>(it)->c_str());
    QString text = QString::fromStdString(*it);
} else if constexpr (typeid(*list) == typeid(vector<int>)){
    QString text = QString::number(*it);
}

And now the code will only compile if the condition is true.

If you do not have access to C++17 you will need to split the function into overloads/specializations to keep the type dependent code separated.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402