Mason R Mason R - 3 months ago 18
C++ Question

Declaring Multiple Variables in a For Loop Then assigning those Variable Data

I am not sure how to handle this, whether it is a pointer or not.

for (int i = 0; i < 5; i++)
{
QString companyTaskText = "companyTask" + QString::number(i + 1);
QTextEdit *companyTaskText = new QTextEdit();
compoanyTaskText = "companyNumSoldiers" + QString::number(i + 1);
QComboBox *companyTaskText = new QComboBox();
for (int j = 0; j < 50; j++)
{
____->addItem(QString::number(i + 1));
}
}


So where I am lost is how to point to the QComboBox that was created above in the for loop. I may be just going about this all wrong.

Answer

You can't have two different variables using the same name within the same scope, replace the second and third ones (change companyTaskText to companyTaskCombo/companyTaskString).

for (int i = 0; i < 5; i++)  
{  
    QString companyTaskString = "companyTask" + QString::number(i + 1);  
    QTextEdit *companyTaskText = new QTextEdit();  
    companyTaskString = "companyNumSoldiers" + QString::number(i + 1);  
    QComboBox *companyTaskCombo = new QComboBox();  
    for (int j = 0; j < 50; j++)  
    {  
        companyTaskCombo->addItem(QString::number(i + 1));  
    }
}  
Comments