How to access a vector that was created in a separate header file?

Hi, so i have this vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef new_thing_Inventory_h
#define new_thing_Inventory_h
#include <vector>
#include <string>
using namespace std;

class Inventory
{
public:
    Inventory()
    {
        vector <string> inventory;
        inventory.reserve(25);
    }
};

#endif


that i want to use in another header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef new_thing_Tutorial_h
#define new_thing_Tutorial_h
#include <vector>
#include "Inventory.h"


using namespace std;

class Tutorial
{
public:
    Tutorial()
    {
        Inventory InventAcces;

        cout << "I have given you a sword, now lets equip it" << endl;
        
    }
    
};


#endif


so i know i need to use .push_back or .pop_back, but the program im using dosn't even recognize that inventory is a created vector. what am i doing wrong?
Hi,

There are a bunch of conceptual problems here:

Don't have using namespace std; , especially not in header files, better yet, don't do it ever. Put std:: before each std thing - Google to see why.

5
6
7
8
9
10
11
12
13
14
15
16
using namespace std;

{
class Inventory
{
public:
    Inventory()
    {
        std::vector <std::string> inventory; 
        inventory.reserve(25);
    }
};


inventory is a local variable in the constructor - it won't exist when the constructor is finished. Make it a member variable, and initialise it in the initialiser list (Google that), provide functions to access it where necessary.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef new_thing_Tutorial_h
#define new_thing_Tutorial_h
#include <vector> //only include what you need
#include "Inventory.h"


using namespace std;

class Tutorial
{
public:
    Tutorial()
    {
         // do we actually need this object here
         // again it is local and this is the constructor.
         // better to have another function - send it a const reference to an 
         // Inventory object

        Inventory InventAcces;
        Inventory InventAcces(); // use () to call your constructor  
        cout << "I have given you a sword, now lets equip it" << endl;
        std::cout << "I have given you a sword, now lets equip it\n" ;
        
    }
    
};


#endif 


Try and make the move to having separate .cpp files for each class, rather than inlining every function - name them the same as the class. For example Inventory.cpp The definition of each function including the ctor and dtor go in there.

Cheers
thnx for the help, it is very appreciated
No worries, look forward to seeing your new code :+)

With the files thing, you can get your IDE to make separate files for you, even make stubs in the .cpp file for your functions that you declare in the header file.
Inventory InventAcces(); // use () to call your constructor

What will this program do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

class c
{
public:
	c()
	{
		std::cout<< "c constructor\n";
	}
};

int main()
{
	c o();
	o();
}

c o()
{
	std::cout<< "oh my\n";
}


http://ideone.com/mYGeOC

Edit:
Removed irrelevant link.
Last edited on
> Inventory InventAcces(); // use () to call your constructor

InventAcces is a function taking no arguments and returning a prvalue of type Inventory.

See: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=439

To define an object:
either Inventory InventAcces ; // sane C++
or, if you must: Inventory InventAcces{} ; http://www.stroustrup.com/C++11FAQ.html#uniform-init
Ok, I messed up there quite badly.
Topic archived. No new replies allowed.