0

I've been developing a text-based adventure game, in which I want to store individual items as instances of a few structs: Item, Armor, and Weapon. Problem is, even though my Item struct works, whenever I compile I am getting Armor does not name a type and Weapon does not name a type errors on a couple of const NoArmor/Weapon placeholders. I have checked through other threads on this topic, but none of them have proved to be applicable or useful.

Here is the code I am referring to:

#ifndef ITEMS_H_
#define ITEMS_H_

#include <string>
#include "Types.h"

using namespace std;

struct Item {
    string name;
    double weight;
    double value;
    ItemType type;
};

struct Armor {
    Item base;
    double armorMod;
    WeaponType weakness;
    WeaponType resistance;
    ArmorType armorType;
};

struct Weapon {
    Item base;
    double damageMod;
    double agilityMod;
    WeaponType weaponType;
};

const Item NoItem = {"_NO_ITEM_", 0, 0, NoItemType};
const Armor NoArmor = {NoItem, 0, NoWeaponType, NoWeaponType, NoArmorType};
const Weapon NoWeapon = {NoItem, 0, 0, NoWeaponType};


#endif /* ITEMS_H_ */

EDIT: Types.h is causing the issue

Types.h:

#ifndef TYPES_H_
#define TYPES_H_

enum ItemType {
    NoItemType,
    Food,
    Misc,
    Weapon,
    Armor
};

enum ArmorType {
    NoArmorType,
    Helmet,
    Tunic,
    Chestplate,
    Bracers,
    Gloves,
    Gauntlets,
    Pants,
    Greaves,
    Robes,
    Sabatons,
    Boots
};

enum WeaponType {
    NoWeaponType,
    Sword,
    Dagger,
    Hammer,
    Axe,
    Staff,
    Bow
};

enum ClassType {
    NoClassType,
    Knight,
    Warrior,
    Thief,
    Blacksmith,
    Lumberjack,
    Mage,
    Archer
};

enum RaceType {
    NoRaceType,
    Human,
    WoodElf,
    Orc,
    DarkElf,
    Dragonling,
    Dwarf
};

enum SpecialtyType {
    NoSpecialtyType,
    TwoHand,
    OneHand,
    Archery,
    Necromancy,
    Conjuration,
    Destruction
};



#endif /* TYPES_H_ */

Solved: Issue was the ItemValue enum containing identical values (Weapon and Armor).

Noxilus
  • 25
  • 1
  • 10
  • 2
    Your objects `NoItem`, `NoArmor`, and `NoWeapon` seem to be intended as globals. Therefore they need [external linkage](https://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage-in-c). – Henri Menke Mar 30 '17 at 03:07
  • Is this a header file or a source (`.cpp`) file? If it's a header file, don't include variable value definitions or `using namespace` statements. – Dai Mar 30 '17 at 03:07
  • 1
    I can't replicate the error. Visual Studio 2015. I filled in enums that just provide the "NoItemType" etc, and a main.cpp that only provides the bare bones int main, etc. Compiles and runs fine. Maybe strip yours down and test it, then add in the other stuff you're doing. – Jason Lang Mar 30 '17 at 03:12
  • @HenriMenke tried this but no cigar. @Dai didn't even realize that the namespace code snuck in there, removed. Upon defining the three in `Items.cpp`, however, I got some issues with the compiler not recognizing the variables themselves as types (even with the extern). To both of you, with the changes combined for whatever reason the compiler is claiming that `string` isn't a type either. @JasonLang I will see what I can do about pulling it apart a bit. – Noxilus Mar 30 '17 at 03:19
  • Which line are you getting error, Post full error without editing – Sniper Mar 30 '17 at 03:22
  • @Sniper With the original code I posted: `'Armor' does not name a type` at the line `const Armor NoArmor = {NoItem, 0, NoWeaponType, NoWeaponType, NoArmorType};` – Noxilus Mar 30 '17 at 03:27
  • Change it to "const struct Armor NoArmor" – Sniper Mar 30 '17 at 03:31
  • @Sniper syntactically incorrect. I've already declared Armor to be a struct, and am merely creating an instance of it. – Noxilus Mar 30 '17 at 03:36
  • You're more likely to get a useful answer if you post a [mcve] that reproduces the problem. That way everyone can see for themselves the error you are getting (and can experiment with your code without having to guess at the missing parts. – Frank Boyne Mar 30 '17 at 03:57
  • Please include Types.h as well; also verify that you still get the error with a minimal main.cpp which includes only this file – harmic Mar 30 '17 at 04:08
  • @FrankBoyne @harmic I've isolated the issue to be `Types.h`, updating my original post. – Noxilus Mar 30 '17 at 04:10
  • Re "string isn't a type", you need `std::string`. – aschepler Mar 30 '17 at 04:19
  • @aschepler Yep, fixed already. – Noxilus Mar 30 '17 at 04:22

0 Answers0