Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
//Item init here:
Item copperLongsword("Longsword", "Copper", 
"A longblade made of dull copper", 1, Weapon, 1, 0, 1, 1, 1, false);

enum ItemType { 
    Weapon,
    Shield,
    Consumable,
    Necklance,
    Ring,
    Robe,
    Shirt,
    Pants,
    Helmet,
    Chestplate,
    Pauldron,
    Gauntlet,
    Greaves,
    Boots,
};

class Item
{
    private: 
            std::string Name;
            std::string material;
            int dataBaseID;
            int itemVal;
            std::string itemDesc;
            int maxItemCharges;
            ItemType type;
            int weaponDmg;
            int healBase;
            int armorRating;
            int enchantVal;
            int stack;
            int maxStack;
            bool stackable;

    public:
        Item();
        Item(std::string Name, std::string material,
             std::string ItemDesc, int DatabaseID, int ItemType, 
             int Attack, int Defense, int GoldValue, int Stack, 
             int MaxStack, bool Stackable);
        std::string GetItem() {return Name;}
        std::string GetItemDesc() {return itemDesc;}
        int GetDatabaseID() {return dataBaseID;}
        int GetItemType() {return type;}
        int GetDmg() {return weaponDmg;}
        int GetArmorRating() {return armorRating;}
        int GetItemVal() {return itemVal;}
        int GetStack() {return stack;}
        int GetMaxStack() {return maxStack;}
        bool GetStackable() {return stackable;}
};

std::vector<Item> inventory;

Above is the code relating to Items in my text-based adventure game. Everything seems to work but once I run it, it gives:
Functions.h:8:1: error: ‘Item’ does not name a type; did you mean ‘tm’?
    8 | Item copperLongsword("Longsword", "Copper", 
    "A longblade made of dull copper", 1, Weapon, 1, 0, 1, 1, 1, false);
      | ^~~~
      | tm
Functions.h: In member function ‘void Character::levelUp()’:
Functions.h:266:33: error: ‘copperLongsword’ was not declared in this scope
  266 |             inventory.push_back(copperLongsword);
      |                                 ^~~~~~~~~~~~~~~
➜ 

I have no idea why this is not allowing me to create this item. Any help would be appreciated!

What I have tried:

Google, and various other sites.
Posted
Updated 12-Sep-23 9:10am
v2

The declaration of the copperLongsword function needs to come after the definition of the Item class. Where it is, the compiler doesn't know what the Item class is.
 
Share this answer
 
Comments
CPallini 11-Sep-23 3:04am    
5.
I just gave you a link to C++ tutorials in the question at Text based inventory[^]. Judging by what you have written above, you need to spend some time working through it. I am sorry if you think that is patronising, but if you do not understand the basics of C++ then you are wasting your time trying to write a somewhat advanced application.
 
Share this answer
 
Comments
CPallini 11-Sep-23 3:04am    
5.
Richard MacCutchan 11-Sep-23 3:24am    
Thanks, but it's a waste of time as OP does not accept our advice.
Rick has already written that the place where the declaration of copperLongsword is inappropriate. This causes the first error location. I would recommend keeping the instance of the class local.
C++
int main()
{
    std::cout << "Stating Game!\n";

    std::vector<Item> inventory;

    //Item init here:
    Item copperLongsword("Longsword", "Copper", "A longblade made of dull copper", 1, Weapon, 1, 0, 1, 1, 1, false);

    return 0;
}

After that the implementation of the constructors is missing. Either you forgot to include it, or it is actually missing, which will prevent the linker from resolving the reference. For the other error places code is missing.

Note: It is inconvenient to give member variables the same name as the function's parameters.

There are some more problems, e.g. ItemType cannot be the type and the name of a variable at the same time.

Another error occurs when trying to return an enum as int.
C++
// int GetItemType() { return type; }
ItemType  GetItemType() { return type; }
 
Share this answer
 
v4
Comments
CPallini 11-Sep-23 3:04am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900