P1C Unrelated
P1C Unrelated

Reputation: 221

Random string from array

I cannot define a string array for unknown reasons on C++. I need to randomly get a string from this array.

Been trying the following:

string bands[] = { "Rammstein", "Slipknot", "Franz Ferdinand", "Gorillaz" };

I'm getting the following as error:

error C2146: syntax error : missing ';' before identifier 'bands'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C3845: 'SpurdoSparde::Form1::bands': only static data members can be initialized inside a ref class or value type

Just a reminder, I'm using a Windows forms applications. Not sure if it makes any difference whatsoever.

Upvotes: 0

Views: 1756

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110648

You either didn't include #include <string> or need to add the std:: namespace qualifier to your type:

std::string bands[] = { ... };

Prefer this over doing using namespace std;.

Also, you might should prefer to use a std::vector rather than a plain old C-style array:

std::vector<std::string> bands = { ... };

Upvotes: 1

juanchopanza
juanchopanza

Reputation: 227370

It seems you are not including string and/or not using std::string. The following works:

#include <string>

int main()
{
  std::string bands[] = { "Rammstein", "Slipknot", "Franz Ferdinand", "Gorillaz" };
}

If you are after a dynamically sized collection of strings, you should prefer std::vector<std::string> over a C-style array. If you need fixed size, have a look at std::array<std::string, N> or tr1 or boost alternatives if you don't have C++11.

Upvotes: 2

Related Questions