0

I my trying to make a simple d&d game and I hit a point where it is giving me these errors Invalid use of non-static data member 'health' and several other class members. how do I fix this problem? here's my code.

void no(){
    time_t current_time = time(NULL);
    srand((unsigned) time(NULL));

    int bugstar = (((rand() % 8) + (rand() % 8)) + 2);
    int bujaveline = (((rand() % 2) + (rand() % 2)) + 4);
    int bugjaveline2 = ((rand() % 6) + 2);
    int bugstarboss = (((rand() % 8) + (rand() % 8)) + 3);
    int bujavelineboss = (((rand() % 2) + (rand() % 2)) + 3);
    int bugjavelineboss2 = ((rand() % 6) + 3);
}

int main()
{
BugBearBoss{
    health = 65;//error here
    armour = 17;//error here
    attack = "bugstarboss"; //error here
    attack1 = "bujavelineboss";//error here 
    attack2 = "bugjavelineboss2";//error here
}
BugBear{
    health = 27;//error here
    armour = 16;//error here
    attack = "bugstar";//error here
    attack1 = "bujaveline";//error here
    attack2 = "bugjaveline";type here//error here
}}

these are my classes for each of the items that contain the errors here is the code for the classes.

using namespace std;

class BugBearBoss{
public:
    string weakness;
    int health;
    int armour;
    int attack;
    int attack1;
    int attack2;
};

class BugBear{
public :
    string weakness;
    int health;
    int armour;
    int attack;
    int attack1;
    int attack2;
};

I've tried making the void function a int function and and removing int main but each has its own set of errors.

now I get use of undeclared identifier BugbearBoss, as well as invalid use of non static data member "armour"

  • 1
    Why are all these things strings? They should probably be `enum` or `int` or some other structure/class you declare, like `std::vector`. – tadman Jun 23 '23 at 14:00
  • 5
    The fields aren't static, so they belong to individual instances, not the class type. So, since static wouldn't seem sensible here, you need to instantiate a member of the class and set them. Default access to a class is also private, so either make them public or write setters or constructors to set them. – lastchance Jun 23 '23 at 14:01
  • Asking this is a sure sign you need [a good C++ reference](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) as that is not how you assign to individual fields. You need an instance of a given class as defined, and then, and *only* then, can you write to properties. Remember, a class can behave like a "group of contextually bound variables", but without that context (class instance) you have nothing. – tadman Jun 23 '23 at 14:02
  • Your `no()` function also does absolutely nothing useful. It generates a bunch of random numbers and throws those in the trash. – tadman Jun 23 '23 at 14:03
  • **WARNING**: Using [`rand()` is not a great idea](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) and you’re strongly encouraged to use an appropriate [random number generator facility in the Standard Library](http://en.cppreference.com/w/cpp/numeric/random) that produces high-quality random values. Using `time(NULL)` as a random number will produce identical results if run in the same second, and on many platforms `rand()` is *barely* random at all. – tadman Jun 23 '23 at 14:04
  • 3
    Good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Jun 23 '23 at 14:31
  • 1
    Tactical note: Unless the different classes have significant behaviour difference consider not using inheritance. If the difference between a bugbear and a bugbear boss is different statistic values and name, make them the same class. You want inheritance when a class can do more or different things like bite/claw/*apply poison*. If it just does and takes more damage use the same class and set the hit points and attack strength stats higher. – user4581301 Jun 23 '23 at 14:41
  • Consider reproducing the code in the posted answer. It is unclear why you are replacing the incorrect code in your question with _different incorrect code_ so many times. – Drew Dormann Jun 23 '23 at 15:23
  • Seriously you need to spend some time learning C++. At the moment you are just throwing together random code that feels right to you. Programming requires more discipline than that, there are rules and you need to learn those rules **by studying a book**, not by writing code and hoping it will work. – john Jun 23 '23 at 15:28
  • *'I've tried making the void function a int function and and removing int main'* You are literally trying random stuff and just hoping it will work. This is not a sensible way to learn how to program. Learn some of the rules and techniques of C++ programming first, and then apply them to the program you want to write. – john Jun 23 '23 at 15:35
  • I this is how I was taught by the program I took to do code in c++ – user22120383 Jun 26 '23 at 17:12

1 Answers1

4

I've tried making the void function a int function and and removing int main but each has its own set of errors.

Those didn't solve the issue because they're not the source of it. You have non-static member variables in your classes. They belongs to instances of the class rather than the class itself.

Now, in your case, they are all private and you have not provided any mechanism to access them, so they are useless. You could write a constructor and a set of member functions to provide an interface to this data, or just make them all public.

class BugBearBoss{
public:
    string weakness;
    string health;
    string armour;
    string attack;
    string attack1;
    string attack2;
};

Or:

struct BugBearBoss{
    string weakness;
    string health;
    string armour;
    string attack;
    string attack1;
    string attack2;
};

If you do this, you can create an instance of the class and assign values to these member variables.

int main() {
    BugBearBoss b;
    b.health = "whatever";
}
Chris
  • 26,361
  • 5
  • 21
  • 42