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.

3
  • 3
    Did you forget std:: or to include <string>? Commented Feb 5, 2013 at 16:40
  • Unrelated to your problem: You should use an std::vector instead of a [] array. Commented Feb 5, 2013 at 16:42
  • Make sure you have #include <string> and prefix string with std:: as in std::string bands[]] = {...} Commented Feb 5, 2013 at 16:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I did include it from the beginning, and then again, I'm using a forms application rather than a command line, so the int main() does not apply, I suppose.
@P1CUnrelated main() is irrelevant here, it could have been any function.
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 = { ... };

11 Comments

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.
vector literal is not quite like that (yet): stackoverflow.com/questions/758118/…
@P1CUnrelated you need #include <vector>. Go to en.cppreference.com and search for "vector".
It's perfectly fine in C++11. It uses std::vector::vector(std::initializer_list<T> init, const Allocator& alloc = Allocator() );.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.