Struggling with inheritance

I'm having trouble inheriting a protected base class member in a derived class.
I'm basically trying to access an int vector called "coords" in my base class. The compiler tells me coords[0] is undefined. Where am I going wrong?

Here's the relevant header section for the base class:
1
2
3
...
     protected:
	std::vector<int> coords;


Here's the relevant info header for the derived class:

1
2
3
4
5
class PlayerPiece : public GamePieces {

   public:
	...
	bool isValidMove ( int input, std::vector< std::vector<char> > &board );


Here's the isValidMove definition in which I'm trying to access the base member variable.

1
2
3
bool PlayerPiece::isValidMove ( int input, std::vector< std::vector<char> > &board ) {
	int playerY = coords[0];
}
It is not a relevant code. Relevant code is the code that everybody can copy and past and after compiling it get the same result as you have.
So create a compiled sample that demonstrates your problem.
Last edited on
Have you stored something in the vector before trying to read it (using push_back or some other method)?

@Kieth89
Have you stored something in the vector before trying to read it (using push_back or some other method)?


It is not important whethet the vector contains something or not because according to the author of the thread the error message says about undefined name.
Last edited on
It compiles for me. Check your base class again.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class GamePieces
{
 protected:
	std::vector<int> coords;
};

class PlayerPiece : public GamePieces {

   public:
	bool isValidMove ( int input, std::vector< std::vector<char> > &board );
};

bool PlayerPiece::isValidMove ( int input, std::vector< std::vector<char> > &board ) {
	int playerY = coords[0];
}
Thanks for your help everyone. I figured out the issue: I didn't have the base class header file included in the subclass header. I'm still new at this, and thought I was misunderstanding inheritance. @vlad, I'll include a more complete code next time. Thanks again.
Topic archived. No new replies allowed.