Vector does not keep values?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 682 Views - Last Post: 04 January 2013 - 10:37 AM Rate Topic: -----

#1 lscorpionlmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-January 13

Vector does not keep values?

Posted 04 January 2013 - 05:55 AM

Hi,

I'm new at the forum and at c++ programming.

I have an object vector, but its not storing the objects.

Don't know what to do anymore. :sad3:

Can someone help me?


teste.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
#include "Player.h"

Player character;

int main(){
character.getMochila().addToBag(1);
std::cin.get();
return 0;
}





Player.h
#ifndef _PLAYER_H_
#define _PLAYER_H_
#include "Bag.h"

class Player
{
    Bag inventory;
public:
    Bag getMochila(){
        return this->mochila;
    }
};
#endif





Bag.h
#ifndef _BAG_H_
#define _BAG_H_
#include "Item.h"

class Bag
{   
    std::vector<Item> usable;

 public:
    void addToBag(int id){
        usable.push_back(loadItem(id));
    }
    
    Item loadItem(int id){
        int i;
        std::string name;
        int hp,mp;
        std::string none;
        std::ifstream teste("Itens.gme");
        while(std::getline(teste,none,'#')){
            i = atoi(none.c_str());
            if(i == id){
                std::getline(teste,none,'{');
                std::getline(teste,name,',');
                std::getline(teste,none,',');
                hp = atoi(none.c_str());
                std::getline(teste,none,',');
                mp = atoi(none.c_str());
                break;
            }
        }
        teste.close();
        Item temp(i,name,hp,mp);
        return temp;
    }
};
#endif





Item.h
#ifndef _ITEM_H_
#define _ITEM_H_

class Item
{
    int id;
    std::string name;
    int hp;
    int mp;
    int qnt;
public:
    Item(){}
    Item(int id, std::string name, int hp, int mp, int qnt = 1){
        this->id = id;
        this->name = name;
        this->hp = hp;
        this->mp = mp;
        this->qnt = qnt;
    }


//-------------------------------GETS-------------------------------------//  
    int getId(){
        this->id;
    }
    std::string getName(){
        this->name;       
    }
    int getHp(){
        this->hp;
    }
    int getMp(){
        this->mp;
    }
    int getQnt(){
        this->qnt;
    }
//-------------------------------SETS-------------------------------------//  
    void setQnt(int qnt){
        this->qnt = qnt;
    }
};


#endif




Is This A Good Question/Topic? 0
  • +

Replies To: Vector does not keep values?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Vector does not keep values?

Posted 04 January 2013 - 06:00 AM

getMochila returns the Bag by value, so you're adding an element to a copy of the bag. If you later call getMochila again, you'll get another copy of the unchanged original bag.
Was This Post Helpful? 1
  • +
  • -

#3 lscorpionlmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-January 13

Re: Vector does not keep values?

Posted 04 January 2013 - 06:03 AM

variable name its not an error, just when rewriting here i put "inventory" when it was "mochila".

just adding, when printing the vector size after calling "addToBag" it says that it's empty.

but how could i get the same mochila every time i call get?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Vector does not keep values?

Posted 04 January 2013 - 06:15 AM

View Postlscorpionlmi, on 04 January 2013 - 02:03 PM, said:

but how could i get the same mochila every time i call get?


By returning by reference instead of by value.

Alternatively you could add a giveItem method to the Player class and never modify the Bag returned by getMochila directly.
Was This Post Helpful? 1
  • +
  • -

#5 lscorpionlmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-January 13

Re: Vector does not keep values?

Posted 04 January 2013 - 06:23 AM

Thanks it helped a lot :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 lscorpionlmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-January 13

Re: Vector does not keep values?

Posted 04 January 2013 - 07:22 AM

don't know why but it seems like the constructor of item is not working...

I tried to create
test(0,std::string(),0,0)
but when print test.getId() it prints an random value.



Tks,
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Vector does not keep values?

Posted 04 January 2013 - 07:23 AM

Please post your current code that is causing the problems.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 lscorpionlmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-January 13

Re: Vector does not keep values?

Posted 04 January 2013 - 07:44 AM

teste.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
#include "Player.h"

Player character;

int main(){
    std::cout << character.mochila.getItem().getId() << std::endl;
    std::cin.get();
    return 0;
}






Player.h
#ifndef _PLAYER_H_
#define _PLAYER_H_
#include "Bag.h"
	 
class Player
{
    Bag mochila;

public:
    Bag getMochila(){
        return this->mochila;
    }
};
#endif





Bag.cpp
#ifndef _BAG_H_
#define _BAG_H_
#include "Item.h"
	 
class Bag
{  
    Item test(0,std::string(),0,0);
public:
    Item getItem(){
        return this->test;
    }






Item.h
#ifndef _ITEM_H_
#define _ITEM_H_

class Item
{
    int id;
    std::string name;
    int hp;
    int mp;
    int qnt;
public:
    Item(){}
    Item(int id, std::string name, int hp, int mp, int qnt = 1){
        this->id = id;
        this->name = name;
        this->hp = hp;
        this->mp = mp;
        this->qnt = qnt;
    }


//-------------------------------GETS-------------------------------------//  
    int getId(){
        this->id;
    }
    std::string getName(){
        this->name;       
    }
    int getHp(){
        this->hp;
    }
    int getMp(){
        this->mp;
    }
    int getQnt(){
        this->qnt;
    }
//-------------------------------SETS-------------------------------------//  
    void setQnt(int qnt){
        this->qnt = qnt;
    }
};


#endif



Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Vector does not keep values?

Posted 04 January 2013 - 08:16 AM

You should enable (and read) warnings for your code. Your getter methods don't have any return statements (which you definitely will get warnings about if they're enabled), so calling them will invoke undefined behavior, causing the random values you're seeing.

Also I'm not able to compile your code at all. Item test(0,std::string(),0,0); is not legal syntax inside a class definition (unless that's part of the new member initialization feature in C++11 and gcc hasn't implemented that part yet). You should use the constructor's initializer list for that.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Vector does not keep values?

Posted 04 January 2013 - 08:23 AM

In your Bag class what exactly do you think the following line is doing?
    Item test(0,std::string(),0,0);

You don't declare an instance of a class like this.
In main what do you think the following line will do?
std::cout << character.mochila.getItem().getId() << std::endl;

That is not how you use class instances.

In your class member functions you shouldn't normally use the this-> notation.
    Bag getMochila(){
        return this->mochila;
    }

Your member function has full access to all members of your class without the need for the "this->".
    Bag getMochila(){
        return mochila;
    }



I suggest you study up on how to properly create and use classes. Start with these links:
Classes I and Classes II.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Vector does not keep values?

Posted 04 January 2013 - 08:29 AM

View Postjimblumberg, on 04 January 2013 - 04:23 PM, said:

In main what do you think the following line will do?
std::cout << character.mochila.getItem().getId() << std::endl;

That is not how you use class instances.


Except for the fact that it's accessing a private variable¹, I don't see what's wrong in that line.

¹ I bet that's another typo and he called the getter method in his real code - if so, the OP should definitely start making sure that his changed/simplified code actually still compiles before he posts it.
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,363
  • Joined: 20-March 10

Re: Vector does not keep values?

Posted 04 January 2013 - 08:36 AM

Hmm,

This is a lesson in obfuscation...

and how not to write clear and precise code...

Why do thing this way in the first place ??


Are you a cryptographer ??

deriving one class from another must have a reason to it

say I have a class 'shape'

and I derive Circle, Triangle and Square classes from the original 'shape' class

That would be a sensible and proper use of classes and inheritance.

But this just seems to be an exercise in confusion.


So much so you managed to confuse yourself in the process.


Why not implement one class that does the same thing but in an easy to understand way..???

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Vector does not keep values?

Posted 04 January 2013 - 08:41 AM

View Postsnoopy11, on 04 January 2013 - 04:36 PM, said:

deriving one class from another must have a reason to it


Are you sure you posted in the right thread? There's no inheritance anywhere in the OP's code.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,682
  • Joined: 25-December 09

Re: Vector does not keep values?

Posted 04 January 2013 - 08:43 AM

Well since mochila is private, you can't access that variable outside the class. So I stand by my assertion:

Quote

That is not how you use class instances.


My point is that the OP needs a better understanding of how a single class works before trying to use multiple intertwined classes.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 lscorpionlmi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-January 13

Re: Vector does not keep values?

Posted 04 January 2013 - 09:37 AM

sorry for this confusion guys, ill post the entire code.

it's very extensive. I'm compiling it with devc++ and it compiles fine, except that it doesn't get the id 0 I'm expecting.

LearningC++.rar

Sorry if it's not that readable, still applying good practices.

Thanks

the tests are indentified in jogo.cpp and bag.h
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2