0
votes

i have quite strange problem.

I have 3 files:
figure.h:

#ifndef FIGURE_H
#define FIGURE_H
namespace figure
{
    class figure
    {
        public:
            figure(position &p,color c);
            virtual bool canMove(const position &p)=0;
            virtual bool move(const position &p)=0;
        protected:
            color col;
            position &p;
    };
    class king : public figure
    {
    };
};
#endif // FIGURE_H

king.h:

#ifndef KING_H
#define KING_H

#include "./figure.h"
namespace figure
{
   class king : protected figure
   {
   };
}
#endif // KING_H

and king.cpp:

#include "king.h"
bool figure::king::canMove(const position &p)
{
}

I'm compiling it with: gcc -std=c11 -pedantic -Wall -Wextra

but the problem is that i got this error:

/src/figure/figure.h:24:45: error: no ‘bool figure::king::canMove(const position&)’ member function declared in class ‘figure::king’

What should I do? Thank you very much!

3
Namespaces and function bodies don't need semicolons after them. - chris
@chris - post that as an answer - Zachary Kniebel
@ZacharyKniebel, I highly doubt that causes the error. - chris

3 Answers

5
votes

You need to declare that function in class king.

class king : public figure
{
  virtual bool canMove(const position &p) override;  // This was missing.
};

Edit:

All derived classes must implement abstract functions if I'm not mistaken

That is incorrect. You may want class king to also be an abstract class. As with other class members, omitting the declaration above tells the compiler that king::canMove should inherit from figure::canMove - that it should still be pure virtual.

That's why you need the declaration above.

0
votes

As the error message says, you haven't declared the method canMove(). Just declare it in class king

namespace figure
{
   class king : public figure
   {
   public:
       bool canMove(const position &p); 
   };
}
0
votes

As indicated by the compiler message, you need to declare canMove(const position&) inside king class.