0

I am attempting to make a text adventure sort of game, and I would like to avoid a bunch of conditionals, so I am trying to learn about the classes stuff and all that. I have created several classes, but the only ones that pertain to this problem are the Options class and the Items class. My problem is that I am trying to push_back() a object into a vector of the type of that object's class and it apparently doesn't happen yet runs until the vector is attempted to be accessed. This line is in main.cpp. I have researched on this, but I have not been able to find a direct answer, probably because I'm not experienced enough to not know the answer in the first place.

The program is separated into 3 files, main.cpp, class.h, and dec.cpp. dec.cpp declares class objects and defines their attributes and all that.

main.cpp:

#include <iostream>
#include "class.h"

using namespace std;
#include <vector>
void Option::setinvent(string a, vector<Item> Inventory, Item d)
{
    if (a == op1)
{
    Inventory.push_back(d);
}
else {
    cout << "blank";
}
return;
}


int main()
{
    vector<Item> Inventory;
        #include "dec.cpp"
    Option hi;
    hi.op1 = "K";
    hi.op2 = "C";
    hi.op3 = "L";
    hi.mes1 = "Knife";
    hi.mes2 = "Clock";
hi.mes3 = "Leopard!!";



        string input1;
    while (input1 != "quit")
{

    cout << "Enter 'quit' at anytime to exit.";

    cout << "You are in a world. It is weird. You see that there is a bed in the room you're in." << endl;
cout << "There is a [K]nife, [C]lock, and [L]eopard on the bed. Which will you take?" << endl;
cout << "What will you take: ";
cin >> input1;
hi.setinvent(input1, Inventory, Knife);
cout << Inventory[0].name;
cout << "test";
}
}

dec.cpp just declares the Item "Knife" and its attributes, I've tried pushing directly and it works, and the name displays.

class.h

#ifndef INVENTORY_H
#define INVENTORY_H
#include <vector>
class Item
    {
    public:
        double damage;
        double siz;
        double speed;
        std::string name;
    };
class Player
{
    public:
    std::string name;
    double health;
    double damage;
    double defense;
    double mana;
};
class Monster
{
    public:
    double health;
    double speed;
    double damage;
    std::string name;
};
class Room
{
    public:
    int x;
    int y;
    std::string item;
    std::string type;
};
class Option
{
    public:
    std::string op1;
    std::string op2;
    std::string op3;
    std::string mes1;
    std::string mes2;
    std::string mes3;
    void setinvent(std::string a, std::vector<Item> c, Item d);
};
#endif

Any help would be greatly appreciated! I realize that the whole structure may need to be changed, but I think that this answer will help even if that may be the case.

  • 6
    You forgot to pass the vector by reference. – NathanOliver Dec 17 '15 at 17:01
  • 1
    It does push_back but you throw the result away by passing by value. – drescherjm Dec 17 '15 at 17:04
  • Thanks for the quick replies! Can you tell me where I would do that? I am not sure how to pass it by reference. –  Dec 17 '15 at 17:06
  • In `void Option::setinvent(string a, vector Inventory, Item d)` you **must** pass `Inventory` by `&` to get the behavior you need. You **should** pass `a` and `d` by `const&` so it should be: `void Option::setinvent(string const& a, vector& Inventory, Item const& d)` – JSF Dec 17 '15 at 17:06
  • 1
    `#include "dec.cpp"` is in a very weird place; nothing is impossible, but it's also likely that you didn't quite understand header files yet. Please check or clarify with a comment if it's intentional – sehe Dec 17 '15 at 17:06
  • Thanks so much guys! I did your suggestion JSF, and after that changed the header to declare it the way you showed me and it worked! :D Thanks so much! –  Dec 17 '15 at 17:10

1 Answers1

1

My problem is that I am trying to push_back() a object into a vector of the type of that object's class and it apparently doesn't happen yet runs until the vector is attempted to be accessed.

it happen but only inside your setinvent method:

void Option::setinvent(string a, vector<Item> Inventory, Item d)
                                 ^^^^^^^^^^^^ - passed by value

Inventory is passed by value which means it is a local vector variable in setinvent function. If you want to modify vector from main function, make it a reference:

void Option::setinvent(string a, vector<Item>& Inventory, Item d)
                                 ^^^^^^^^^^^^ - passed by reference, modifies vector from main

now Inventory is local reference variable. Also dont forget to change setinvent declaration in header file.

marcinj
  • 48,511
  • 9
  • 79
  • 100