-1

I'm new to coding in C++ and more used to some simple Java programming.

I made two classes, class A(int) and class B. I made an array filled with names, and I want int A to say one of the names from class B.

Example:

Class B {

   static string NPCnames[] { "John", "Mike", "Alex", "Adam"};
};

int A {

    cout << B::NPCnames[rand()]

};

It comes up with errors for the arrays (because I'm new I don't understand them)

Error message:

Error 1 error C2864: 'Data::NPCnames' : a static data member with an in-class initializer must have non-volatile const integral type

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Venture13
  • 15
  • 4

1 Answers1

4

You need...

class B { static string NPCnames[4]; };

string B::NPCnames[4] = { "blah",  "blah", ... };
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • thanks, but what if i were to replace the 4 in the `B::NPCnames[4]` with rand()? – Venture13 Dec 17 '15 at 23:17
  • Use a constructor and an additional variable representing the array size. – Ziezi Dec 17 '15 at 23:19
  • 1
    @Venture13 - then you'd have serious problems. – Edward Strange Dec 17 '15 at 23:19
  • ok so then how about `B::NPCnames[rand() % 4]` also, i want the program to print out the `NPCnames` string. side-note: sorry for not understanding programming, like i said in the beginning of this post `i'm new to coding` also, im new to this site. – Venture13 Dec 17 '15 at 23:26
  • @Venture13 The size of the array `NPCnames` cannot be variable. C++ forbids variable length arrays on the stack. – Fantastic Mr Fox Dec 17 '15 at 23:26
  • I guess here is a misunderstanding. @Venture13 You want to access a random element from the array. The `[4]` in the above code is the size of the array, which should not be random. – leemes Dec 17 '15 at 23:39
  • thanks @leemes also, i'll just send the code so everyone knows what's not working [Code](http://www.hastebin.com/izaxapulin.vala) – Venture13 Dec 17 '15 at 23:46