Problems with vectors

Hello, guys I've been writing a ASCII rouge-like recently, when I stumbled on a strange bug.

I have declared a vector containing a class I made.
 
  vector <item> items;


This line gives me some weird errors.
1
2
3
  for (int i = 0; i < items.size; i++){
    //code here
  }


Here are the errors:
 
error C3867: 'std::vector<item,std::allocator<_Ty>>::size': function call missing argument list; use '&std::vector<item,std::allocator<_Ty>>::size' to create a pointer to member

 
error C2297: '<' : illegal, right operand has type 'unsigned int (__thiscall std::vector<item,std::allocator<_Ty>>::* )(void) throw() const'


I'm running Virtual Studio 2013 if that will help.

Ty in advance,
b100
it's items.size() not items.size. These are functions.
Thanks TarikNeaj!
For some reason I get an undeclared identifier error when I declare my vector.
I'm sure I included "item.h thouh.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include<iostream>
#include<vector>

#include"item.h"
using namespace std;

class player {
public:
	player();
	int health;
	int speed;
	int attack;
	string name;
	vector <item> items;
	void printStats();
	void printItems();
};
Last edited on
Please post the contents of item.h, and the complete error message, all of them exactly as they appear in your development environment.

And also note you shouldn't be using the "using namespace std;" clause inside your header files. Instead properly scope the std::namespace.


item.h:
1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include<string>
#include"player.h"
using namespace std;

class item {
public:
	item(string n, vector<pair <int, string>> st);
	vector <pair <int, string>> stats;
	string name;
	void applyStats(player p);
};

error:
 
error C2065: 'item' : undeclared identifier
]
That's not the complete error message. These error messages contain important information to locate and fix the problem. Your truncated error message is missing several pieces of this important information, for example the message should tell you the file and the line number within that file where the error was detected.

Also note that your item.h is missing the include files for both the std::vector and the std::pair<> classes. You should always #include all the required files and not try to rely on some other mysterious file to include these headers.

Lastly, since you're not using constructor initialization lists you will probably need a no argument constructor for your item class.
Here is the complete copy of the error message:
 
Error	3	error C2065: 'item' : undeclared identifier	c:\users\blank\documents\visual studio 2013\projects\2drougelike\2drougelike\player.h	Line:15	Column:1	2DRougeLike


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include<iostream>
#include<vector>

#include"item.h"
using namespace std;

class player {
public:
	player();
	int health;
	int speed;
	int attack;
	string name;
	/*error*/ vector <item> items;
	void printStats();
	void printItems();
};


And I am using constructor initialization as you can see on line 8.
Last edited on
And I am using constructor initialization as you can see on line 8.

Do you mean this line? :
item(string n, vector<pair <int, string>> st);
If so this line doesn't show the use of an initialization list.

http://www.learncpp.com/cpp-tutorial/8-5a-constructor-member-initializer-lists/


However to fix this program it would probably be better to declare a no argument constructor for both your player and item classes.

By the way thank you for the complete error message as it shows the actual file (player.h) and line (15).
Last edited on
You have a circular dependency. player.h includes item.h, but item.h includes player.h.

I'm guess that, in the source code file that's generating the error, it's "item.h" that is included first. Since "player.h" is included before the item class is defined, the compiler is processing the definition of player before it processes the definition of item. This means that, when processing the definition of player, item is undefined.

Edit: You'll need to decide which way around you want your dependencies to be, and then use forward declaration to resolve the problem.

Also, what's the point of having a class where the data members are public? You're breaking encapsulation, which is one of the major reasons to use a class in the first place.
Last edited on
That has nothing to do with my undeclared identifier error tough.
Of course it does.

This means that, when processing the definition of player, item is undefined.
Last edited on
Sorry dude my reply was to the previous poster didn't see you there. Thanks for the help! I was able to fix the issue so I'm marking as solved. Would love if you explain why should I make setters and getters, and not just use public variables.
That's something that should be covered by any basic tutorial or textbook chapter on OOP/classes. Encapsulation is one of the basic pillars of OOP.
Topic archived. No new replies allowed.