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.
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"
usingnamespace std;
class player {
public:
player();
int health;
int speed;
int attack;
string name;
vector <item> items;
void printStats();
void printItems();
};
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.
#pragma once
#include<iostream>
#include<vector>
#include"item.h"
usingnamespace 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.
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.
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.