I have multiple errors in my code that I was just working on, the errors are as follows:

Here is my build log:


monsters.h:11: error: storage class specified for field `enemyattack'
monsters.h:11: error: ISO C++ forbids initialization of member `enemyattack'
monsters.h:11: error: making `enemyattack' static
monsters.h:11: error: ISO C++ forbids in-class initialization of non-const static member `enemyattack'
monsters.h:12: error: storage class specified for field `enemyhealth'
monsters.h:12: error: ISO C++ forbids initialization of member `enemyhealth'
monsters.h:12: error: making `enemyhealth' static
monsters.h:12: error: ISO C++ forbids in-class initialization of non-const static member `enemyhealth'
monsters.h:13: error: storage class specified for field `enemydefense'
monsters.h:13: error: ISO C++ forbids initialization of member `enemydefense'
monsters.h:13: error: making `enemydefense' static
monsters.h:13: error: ISO C++ forbids in-class initialization of non-const static member `enemydefense'
monsters.h:14: error: missing terminating ' character
monsters.h:15: error: expected primary-expression before '}' token
monsters.h:15: error: storage class specified for field `enemy'
monsters.h:15: error: ISO C++ forbids initialization of member `enemy'
monsters.h:15: error: making `enemy' static
monsters.h:15: error: ISO C++ forbids in-class initialization of non-const static member `enemy'
monsters.h:15: error: expected `;' before '}' token
monsters.h:15: error: expected `;' before '}' token

main.cpp: In function `int main()':
main.cpp:14: error: `enemyhealth' undeclared (first use this function)
main.cpp:14: error: (Each undeclared identifier is reported only once for each function it appears in.)

main.cpp:15: error: `enemyattack' undeclared (first use this function)
main.cpp:16: error: `enemydefense' undeclared (first use this function)


And here is my code

main.cpp

#include <iostream>
#include <fstream>
#include <math.h>
#include "monsters.h"
using namespace std;
char enemy[50];
char command[35];


int main() {
    system("color 02");
    strcpy(enemy,"Nathan Archer"); 
    cout << "Fight with " <<enemy;
    cout << "\nHealth: " <<enemyhealth;
    cout << "\nAttack: " <<enemyattack;
    cout << "\nDefense: " <<enemydefense; 
    cout << "\n \n \n";
    cout << "Commands: \n Attack \n Magick \n Run!";
    cout << "\n ====================";
    cout << "\n | Command:";
    
    
    cin.getline(command, 35);
    
    
    return 0;
}

monsters.h

#include <string>


#ifndef MONSTERS_H
#define MONSTERS_H



struct NArcher {
  extern int enemyattack = 10;
  extern int enemyhealth = 20;
  extern int enemydefense = 5;
  extern char enemy = 'Nathan Archer;
} ;


#endif

Thanks ahead of time

This piece of code makes no sense:

struct NArcher {
  extern int enemyattack = 10;
  extern int enemyhealth = 20;
  extern int enemydefense = 5;
  extern char enemy = 'Nathan Archer';
} ;

First with the obvious, if "enemy" is supposed to store a string, it should be a string, not a char. You can either have char* enemy = "Nathan Archer"; or std::string enemy = "Nathan Archer"; (notice the double quotes for defining a string, the single quote is only for defining a single character).

Now for the more important problem, i.e., your use of the extern keyword. First, you have to understand the fundamental difference between a data member, a static data member and a global variable. A data member, like "foo" in:

struct myClass {
  int foo;
};

is a variable that is within an object of the class (myClass). This means that when you create an object of that class, e.g. myClass myObj; , you can access the data member inside the object with myObj.foo . This means that you have one such data member inside each object (each with its own value).

A static data member, as "bar" in:

struct myClass2 {
  static int bar;
};

is essentially a global variable that is associated to the class it is declared in. This means that there is one such variable in the whole program, regardless of how many objects of that class you have (or none). So, since a static data member is associated to a class and not to an object, all you need to access that variable is to specify the enclosing class, as myClass2::bar .

A global variable, as "foobar" in:

int foobar;

is a variable that is unique in the whole program and is not associated with any object or class. So, you can access those as simple foobar (like you would with any other variable).

Now, the extern keyword, when qualifying a variable declaration, tells the compiler that the variable in question is only declared here and that its definition (the actual variable) must be sought elsewhere (and thus, "externally") in the rest of the code (usually in a linked object file). So, first of all, this excludes data members as candidates for being "extern" because they are always associated to an object. Second, the use of "extern" also implies that you cannot initialize the variable in the same statement, as in extern int foobar = 4; is not allowed. This is because the extern declaration is simply an indication to the compiler that this variable exists somewhere, it is not the definition of that variable (i.e. either you have to look elsewhere for the definition (and use 'extern') or you have the definition right there (and don't use 'extern'), these are mutually exclusive options).

Now, another thing is that you cannot (at least, not until very recently) initialize a (non-static) data member at the declaration site. You can only do so for static data members and global variables.

So, your compiler goes crazy with the syntax that you posted, and for good reason:

>>error: storage class specified for field `enemydefense'

This is due to the fact that 'extern' is not possible for (non-static) data members (i.e. 'extern' is a type of "storage class").

>>error: ISO C++ forbids initialization of member `enemydefense'

This is due to the fact that you cannot initialize a (non-static) data member at the declaration-site (must be done in a constructor).

>>error: making `enemydefense' static

This is due to the fact that the compiler wants to keep looking at more code (to see if the rest compiles) so it will mark the 'enemydefense' data member as 'static' because it assumes that you just forgot to do so yourself (because otherwise it wouldn't make sense to have an initialization at the declaration-site).

>>error: ISO C++ forbids in-class initialization of non-const static member `enemydefense'

This is due to the fact that even once your 'enemydefense' data member is made 'static', it is still forbidden to do an in-class initialization of it, because it is not 'const' (a constant value).


I cannot really help you to fix it, because I don't know what you want (a global variable, a static data member, a static const data member, a non-static data member, an extern global variable, etc.). Please give details on that and we can move towards a solution to your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.