-1

cpp file:

#include "currency.hpp"
currencyNames[4] = {"gbp","usd","eur","aud"};

QComboBox *box1 = new QComboBox();
int i;
for(i=0; i < 4; i++){
    QString *s = QString::string(currencyNames[i]);
    box1->addItem(s);
} 

hpp file:

#pragma once
#include string

.
.
.

static const int SIZE = 4; 
std::string currencyNames[SIZE];

I keep getting a number of errors, I want an array containing the information above then loop through the array adding it to the QComboBox. Have no success. All relevant Qt headers included.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 3
    Two obvious problems: That's not how you initialize an array; And you can't have generic statements outside of functions. Perhaps you need to [read a beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – Some programmer dude Nov 18 '16 at 10:47
  • 1
    Further errors: There is no `QString::string` but `QString::fromStdString`. It doesn't return a pointer, but a `QString` object – king_nak Nov 18 '16 at 10:49
  • 1
    Voted to Close as " too broad". The problem is that you're new to C++, and writing too much code at once. As a result, you have multiple syntax errors in your code, and they start to overlap. At that point the compiler starts having real issues in guessing what you might have meant. If you tried a more minimal example, you'd have know that the `currencyNames[4] =` line outside a function was wrong, and fixed that before trying to use it. – MSalters Nov 18 '16 at 11:14

1 Answers1

1

Besides the issues already stated in previous comments QComboBox::addItem method takes a reference to QString not a pointer.

Since you decided to use Qt framework you can embrace its collections which allows better interoperability with various widgets. Hence, your example could be rewritten a bit simplier. For example:

QStringList currencyNames{"gbp","usd","eur","aud"};
QComboBox *box = new QComboBox();
box->addItems(currencyNames);

Just remember to assign the box to some parent to make it handle the box destruction when appropriate.

Dusteh
  • 1,496
  • 16
  • 21