Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>

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:
        std::string displayName = Name + " " + material;
        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;}
        std::string GetDispName() {return displayName;}
        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;}
};
//Vector init:
std::vector<std::string> inventory;
        
void printVec(std::vector<std::string> vec) {
    for (int i = 0; i < vec.size(); i++) {
        std::cout << vec[i] << " \n";
    }
}

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

int Die(int sides, int minVal) {
    srand((unsigned int) time(NULL));
    int roll = (rand() % sides) + minVal;
    return roll;
}

class Character {
    public:
        Character();
        virtual ~Character();

        void initialize();
        void levelUp();
        void statMods();
        void statDisplay();
        void getName();

        double xPos;
        double yPos;
    private:
        std::string name;
        int warriorLvl, rogueLvl, magicUserLvl;
        int level;
        int exp;
        int expReq;

        int strength;
        int dexterity;
        int constitution;
        int intelligence;
        int wisdom;
        int charisma;

        int strMod;
        int dexMod;
        int conMod;
        int intMod;
        int wisMod;
        int chaMod;

        int skillPoints;
        int statPoints;

        int hp;
        int hpMax;
        int stamina;
        int staminaMax;
        int damageMin;
        int damageMax;
        int armor, shield;
        int armorClass;
};

Character player;

Character::Character() {
    this->xPos = 0.0;
    this->yPos = 0.0;

    this->name = "";
    this->warriorLvl, this->rogueLvl, this->magicUserLvl = 0;
    this->level = 1;
    this->exp = 0;
    this->expReq = 0;

    this->strength = 0;
    this->dexterity = 0;
    this->constitution = 0;
    this->intelligence = 0;
    this->wisdom = 0;
    this->charisma = 0;

    this->strMod = 0;
    this->dexMod = 0;
    this->conMod = 0;
    this->intMod = 0;
    this->wisMod = 0;
    this->chaMod = 0;

    this->skillPoints = 0;
    this->statPoints = 0;

    this->hp = 0;
    this->hpMax = 0;
    this->stamina = 0;
    this->staminaMax = 0;
    this->damageMin = 0;
    this->damageMax = 0;
    this->armor, this->shield = 0;
    this->armorClass = 0;
}

Character::~Character() {

}

void Character::initialize() {
    this->xPos = 0.0;
    this->yPos = 0.0;

    this->name = name;
    this->warriorLvl = 0;
    this->rogueLvl = 0;
    this->magicUserLvl = 0;
    this->level = this->warriorLvl + this->rogueLvl + this->magicUserLvl;
    this->exp = 0;
    this->expReq = (50/3)*(pow(level, 3) - 
                       6*pow(level, 3) + (17*level) - 11);

    this->strength = Die(1, 15);
    this->dexterity = Die(1, 15);
    this->constitution = Die(1, 15);
    this->intelligence = Die(1, 15);
    this->wisdom = Die(1, 15);
    this->charisma = Die(1, 15);

    this->strMod = floor((strength - 10) / 2);
    this->dexMod = floor((dexterity - 10) / 2);
    this->conMod = floor((constitution - 10) / 2);
    this->wisMod = floor((wisdom - 10) / 2);
    this->intMod = floor((intelligence - 10) / 2);
    this->chaMod = floor((charisma - 10) / 2);

    this->skillPoints = 0;
    this->statPoints = 0;

    this->hpMax = 5 + conMod;
    this->hp = hpMax;
    this->staminaMax = 5 + strMod;
    this->stamina = staminaMax;
    this->damageMin = level + dexMod;
    this->damageMax = level + strMod;
    this->armor, shield = 0;
    this->armorClass = 10 + armor + shield;
}

void Character::statMods() {
    this->strMod = floor((strength - 10) / 2);
    this->dexMod = floor((dexterity - 10) / 2);
    this->conMod = floor((constitution - 10) / 2);
    this->wisMod = floor((wisdom - 10) / 2);
    this->intMod = floor((intelligence - 10) / 2);
    this->chaMod = floor((charisma - 10) / 2);

    this->hpMax = 5 + conMod;
    this->hp = hpMax;
    this->staminaMax = 5 + strMod;
    this->stamina = staminaMax;
    this->damageMin = level + dexMod;
    this->damageMax = level + strMod;
    this->armor, shield = 0;
    this->armorClass = 10 + armor + shield;
}

void Character::getName() {
    std::cout << "Enter you name:\n>";
    getline(std::cin, name);
}

void Character::statDisplay() {
    std::cout << "= Character Sheet =" << std::endl;
    std::cout << "= Name: " << this->name << std::endl;
    std::cout << "= Level: " << this->level<< std::endl;
    std::cout << "= Exp: " << this->exp << std::endl;
    std::cout << "= Exp to next level: " 
                       << this->expReq << std::endl;
    std::cout << std::endl;
    std::cout << "= Strength: "<< this->strength << 
                       " - " <<this->strMod << std::endl;
    std::cout << "= Dexterity: " << this->dexterity 
         << " - " << this->dexMod << std::endl;
    std::cout << "= Constitution: " << this->constitution 
    << " - " << this->conMod << std::endl;
    std::cout << "= Intelligence: " << this->intelligence 
    << " - " << this->intMod << std::endl;
    std::cout << "= Wisdom: " << this->wisdom 
    << " - " << this->wisMod << std::endl;
    std::cout << "= Charisma: " << this->charisma << 
    " - " << this->chaMod << std::endl;
    std::cout << std::endl;
    std::cout << "= HP: "<< this->hp << 
    "/" << this->hpMax << std::endl;
    std::cout << "= Stamina: " << this->stamina 
    << "/" << this->staminaMax << std::endl;
    std::cout << "= Damage: " << this->damageMin 
    << " - " << this->damageMax << std::endl;
    std::cout << "= Armor Rating: " << this->armorClass << std::endl;
    std::cout << std::endl;
}

void Character::levelUp() {
    this->exp = 0;
    int levelChoice;
    std::cout << "Pick a level:\n1: Warrior\n2: Rogue\n3: Magic User\n>";
    std::cin >> levelChoice;
    switch(levelChoice) {
        case 1:
            this->warriorLvl++;
            this->strength += 2;
            this->constitution++;
            this->dexterity -= 2;
            this->intelligence--;
            inventory.push_back(copperLongsword.GetDispName());
            break;
        case 2:
            this->rogueLvl++;
            this->dexterity += 2;
            this->charisma++;
            this->strength -= 2;
            this->constitution--;
            break;
        case 3:
            this->magicUserLvl++;
            this->intelligence += 2;
            this->wisdom++;
            this->constitution -= 2;
            this->charisma--;
            break;
    }
    std::cout << "Pick a stat to increase:\n1: 
    Strength\n2: Dexterity\n3: Constitution\n4: Intelligence\n5: 
    Wisdom\n6: Charisma\n>";
    std::cin >> levelChoice;
    switch(levelChoice) {
        case 1:
            this->strength++;
            break;
        case 2:
            this->dexterity++;
            break;
        case 3:
            this->constitution++;
            break;
        case 4:
            this->intelligence++;
            break;
        case 5:
            this->wisdom++;
            break;
        case 6:
            this->charisma++;
            break;
    }
    std::cout << "Would you like to increase a second stat?
    \n*Warning you must also decrease a stat*\n1: Yes\n2: No\n>";
    std::cin >> levelChoice;
    switch(levelChoice) {
        case 1:
            std::cout << "Pick a second stat to 
            increase:\n1: Strength\n2: Dexterity\n3: 
            Constitution\n4: Intelligence\n5: Wisdom\n6: Charisma\n>";
            std::cin >> levelChoice;
            switch(levelChoice) {
                case 1:
                    this->strength++;
                    break;
                case 2:
                    this->dexterity++;
                    break;
                case 3:
                    this->constitution++;
                    break;
                case 4:
                    this->intelligence++;
                    break;
                case 5:
                    this->wisdom++;
                    break;
                case 6:
                    this->charisma++;
                    break;
            }
            std::cout << "Pick a stat to decrease:\n1: 
            Strength\n2: Dexterity\n3: Constitution\n4: 
            Intelligence\n5: Wisdom\n6: Charisma\n>";
            std::cin >> levelChoice;
            switch(levelChoice) {
                case 1:
                    this->strength--;
                    break;
                case 2:
                    this->dexterity--;
                    break;
                case 3:
                    this->constitution--;
                    break;
                case 4:
                    this->intelligence--;
                    break;
                case 5:
                    this->wisdom--;
                    break;
                case 6:
                    this->charisma--;
                    break;
            }
            break;
        case 2:
            break;
    }
    this->level = warriorLvl + rogueLvl + magicUserLvl;
    this->expReq = (50/3)*(pow(level, 3) - 
                       6*pow(level, 3) + (17*level) - 11);
    player.statMods();
}

class Room {
    public:
        std::string roomName, roomNorth, roomEast, roomWest, roomSouth;
        void roomFunction();
};

class Game {
    public:
        Game();
        virtual ~Game();

        void mainMenu();
        void travel();

        inline bool getPlaying() const { return this->playing; }
    private:
        int choice;
        bool playing;
};

Game::Game() {
    choice = 0;
    playing = true;
}

Game::~Game() {

}

void Game::mainMenu() {
    std::cout << "0: QUIT\n1: North\n2: East\n3: 
                        South\n4: West\n5: Stats\n6: Inventory\n>";
    std::cin >> choice;
    switch(choice) {
        case 0:
            this->playing = false;
            break;
        case 1:
            player.yPos += 0.1;
            break;
        case 2:
            player.xPos += 0.1;
            break;
        case 3:
            player.xPos -= 0.1;
            break;
        case 4:
            player.yPos -= 0.1;
            break;
        case 5:
            player.statDisplay();
            break;
        case 6:
            printVec(inventory);
            break;
    }
}

void Game::travel() {

}

The code above is for my MUD game, but when I run it, I get:
cd "/usercode/" && g++ main.cpp -o main && 
"/usercode/"main
/usr/bin/ld: /tmp/ccaICDEk.o: in function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x1642): undefined reference to `Item::Item(std::__cxx11::basic_string<char, 
     std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
int, int, int, int, int, int, int, bool)'
collect2: error: ld returned 1 exit status 

It seems to be a undefined reference error, but I don't know how to fix it. Any help would be appreciated.

What I have tried:

Google-ing, tome of C++ knowledge. I already looked through the albeit limited sites I have at my disposal.
Posted
Updated 12-Sep-23 11:51am
v2
Comments
Richard MacCutchan 11-Sep-23 3:14am    
And you will continue to get such errors until you spend time learning C++ from a proper tutorial. The code above looks like it was just thrown together without any thought of proper structure.

I'll take a stab at it and say that you're trying to call the Item constructor with the wrong parameters.

You have as your constructor parameters;
C++
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);

and you call it with;
C++
Item copperLongsword("Longsword", "Copper", "A longblade made of dull copper", 1, Weapon, 1, 0, 1, 1, 1, false);

Your calling with ItemType as an enum and the constructor is expecting an int.
 
Share this answer
 
Comments
merano99 10-Sep-23 19:40pm    
It gets worse ...
Apparently, the questioner does not read the answers, or not correctly. I had already here
https://www.codeproject.com/Questions/5368041/How-to-fix-item-declaration-problem
written the following:
"ItemType cannot be the type and the name of a variable at the same time."
Mike Hankey 10-Sep-23 19:46pm    
You can lead a horse to water but you can't make him drink.
merano99 11-Sep-23 3:03am    
Top! Yes, that's exactly how it is. I'll remember that saying. You get +5
There are a few problems. Mike mentioned one. Also, the parameterized constructor should have its arguments declared slightly different than they are. I will list them one per line so you can see them more clearly :
C++
Item( std::string name_
    , std::string material_
    , std::string itemDesc_
    , int databaseID_
    , ItemType type_    // <-- this one should be the enumerated type
    , int attack_
    , int defense_
    , int goldValue_
    , int stack_
    , int maxStack_
    , bool stackable_
    );
Also, this line of code is not correct in a class declaration :
C++
std::string displayName = Name + " " + material;
In a declaration it should be :
C++
std::string displayName;
You should be more careful with your variable names. Avoid naming arguments the same as your class' members and methods.

ETA: I just realized I named several arguments the same are your members. This was out of habit because I never, ever name members starting with lower case letters, except the "m_" prefix which they almost always have. If not then they start with a capitalized letter. For this reason, I appended an underscore to them.

BTW - you are not running this code. You are compiling it. As it is, it won't compile so you can't run it yet.
 
Share this answer
 
v3
Comments
merano99 11-Sep-23 3:15am    
I already tried to correct the code in his previous post with the same problem. see there.
Obviously you dear questioner do not read already given answers, or not correctly. I have various problems, also this already pointed out:
https://www.codeproject.com/Questions/5368041/How-to-fix-item-declaration-problem
Mike's saying "You can lead a horse to water but you can't make him drink." applies here fully and I refer to the already given hints by me and others.
 
Share this answer
 
Comments
Richard MacCutchan 11-Sep-23 3:12am    
I have given him the link to C++ tutorials but he obviously cannot be bothered to actually learn the language, and believes that Google can answer his questions.
merano99 11-Sep-23 3:19am    
I have seen it, meanwhile several have tried to help. I give you 5 for your attempt, but have considerable doubt that this time it is useful.
Richard MacCutchan 11-Sep-23 3:26am    
Oh well, we can but try. I have a feeling the this person is going to be added to the list of "help vampires".
Brennon Nevels 11-Sep-23 6:35am    
First a few things:
1. I prefer werewolves.
2. Thank you all for your help but a lot of the stuff (such as the links you have posted) either my brain refuses to get a grasp upon the concept or I'm an idiot, I prefer the former.
3. I have, in fact, read the tutorials, but as Merano99 said I do not understand them and thus probably read them incorrectly.
merano99 11-Sep-23 10:22am    
If I understood correctly, my linked solution attempt was not accepted as a solution because it did not find its way into the brain? I hope that it was not due to an incorrect translation. It would have been clearly better if you had posted corrected code than to ask the same nonsense again as a new question.

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