Reputation: 17
I have a class with a vector of items that I am adding into the player's inventory. When I compile my project, I end up with an error relating to my vector but have no idea what could be causing this error. Do I need to use char variables instead of a string?
Player.cpp
#include <iostream>
#include <vector>
#include "player.h"
using namespace std;
void Player::AddItem(string item)
{
items.push_back(item);
}
void Player::UseItem(string item)
{
if(HasItem(item))
{
items.pop_back();
}
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <vector>
class Player
{
private:
int row;
int col;
int moves;
std::vector<std::string> items();
public:
Player();
int GetRow();
int GetCol();
void SetPosition(int row, int col);
void AddItem(std::string item);
bool HasItem(std::string item);
void UseItem(std::string item);
int GetMoveCount();
void IncrementMoves();
};
#endif
The error I am receiving:
player.cpp: In member function ‘void Player::AddItem(std::__cxx11::string)’:
player.cpp:49:2: error: invalid use of member function ‘std::vector<std::__cxx11::basic_string<char> > Player::items()’ (did you forget the ‘()’ ?)
items.push_back(item);
^~~~~
player.cpp: In member function ‘void Player::UseItem(std::__cxx11::string)’:
player.cpp:72:3: error: invalid use of member function ‘std::vector<std::__cxx11::basic_string<char> > Player::items()’ (did you forget the ‘()’ ?)
items.pop_back();
^~~~~
make: *** [<builtin>: player.o] Error 1
What could be causing this error?
Upvotes: 0
Views: 439
Reputation: 4288
This line
std::vector<std::string> items();
declares a private function called items
that takes no arguments and returns a std::vector<std::string>
.
Change it to
std::vector<std::string> items;
Your error message
player.cpp:49:2: error: invalid use of member function ‘std::vector<std::__cxx11::basic_string<char> > Player::items()’ (did you forget the ‘()’ ?)
Is pretty descriptive on this. It says you have a member function called items()
and thinks you might be calling it wrong.
Upvotes: 2