0

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.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
P1C Unrelated
  • 221
  • 1
  • 2
  • 8

2 Answers2

2

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

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 = { ... };
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • As of your example, I got the following: `error C2039: 'vector' : is not a member of 'std' error C2143: syntax error : missing ';' before '<' error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C2334: unexpected token(s) preceding '{'; skipping apparent function body` Sorry about the formatting, it's really weird for me. – P1C Unrelated Feb 05 '13 at 16:48
  • vector literal is not quite like that (yet): http://stackoverflow.com/questions/758118/c-vector-literals-or-something-like-them – thang Feb 05 '13 at 16:49
  • @P1CUnrelated you need `#include `. Go to http://en.cppreference.com and search for "vector". – juanchopanza Feb 05 '13 at 16:50
  • My bad, I fixed that before posting, but regardless, `'HorseRacing::Form1::bands': only static data members can be initialized inside a ref class or value type`, and `cannot define 'bands' as a member of managed 'HorseRacing::Form1': mixed types are not supported`. It's really weird, I have done this before. – P1C Unrelated Feb 05 '13 at 16:51
  • 1
    It's perfectly fine in C++11. It uses `std::vector::vector(std::initializer_list init, const Allocator& alloc = Allocator() );`. – Joseph Mansfield Feb 05 '13 at 17:07
  • @thang well, if you replace the `...` by `"hello", "world"` then it should work. I see no reason it wouldn't work in C++11. – juanchopanza Feb 05 '13 at 17:07
  • @juanchopanza, that's c++0x isn't it? try changing to c++: http://ideone.com/lr4zaB – thang Feb 05 '13 at 17:15
  • I suspect this is actually the issue that OP is having with his compile error. – thang Feb 05 '13 at 17:21
  • 2
    @thang C++0x is just what C++11 was called when they expected it to be standardized before 2010. C++ on ideone is C++03. – Joseph Mansfield Feb 05 '13 at 17:21
  • @sfrabbit, you're right. forgot it went through. although i think op is using visual studio (or borland?), so he's seeing the same error... – thang Feb 05 '13 at 17:22
  • @thang it is C++11, which is now C++. The `vector` initialization shown is completely correct according to the current standard. So, it works in C++, it does not work in C++03. – juanchopanza Feb 05 '13 at 17:23