-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

9
  • Java tag removed -- what does this question have to do with Java? Commented Dec 17, 2015 at 23:07
  • no, this is a tobac++onist Commented Dec 17, 2015 at 23:08
  • 5
    You should probably start by reading a good book on C++.
    – Paul R
    Commented Dec 17, 2015 at 23:12
  • 1
    C++ (like Java) is case-sensitive. And B::NPCnames[rand()] - come on, at least do read the documentation. Commented Dec 17, 2015 at 23:13
  • 2
    I'm voting to close this question as off-topic because it is not generally useful. Commented Dec 17, 2015 at 23:15

1 Answer 1

4

You need...

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

string B::NPCnames[4] = { "blah",  "blah", ... };
7
  • thanks, but what if i were to replace the 4 in the B::NPCnames[4] with rand()?
    – Venture13
    Commented Dec 17, 2015 at 23:17
  • Use a constructor and an additional variable representing the array size.
    – Ziezi
    Commented Dec 17, 2015 at 23:19
  • 1
    @Venture13 - then you'd have serious problems. Commented Dec 17, 2015 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
    Commented Dec 17, 2015 at 23:26
  • @Venture13 The size of the array NPCnames cannot be variable. C++ forbids variable length arrays on the stack. Commented Dec 17, 2015 at 23:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.