2

I create this function in a class and I receive error: pointer to incomplete class type is not allowed.

As far as I am concerned however it is possible to declare an array of pointers to an object of an abstract class. After that you can pass objects of the derived class in the array. Where is the problem with this code (class Square is abstract):

public:
Player(int,int);
void captureSquare(Square* []);

void Player::captureSquare(Square* allSquares[22])
{
  for(int i=0;i<22;i++){
    Square* OnSquare = allSquares[i];
      if(OnSquare->getID==squarePosition){
        capturedSquare = OnSquare;
        break;
 }
}

}

1
  • 1
    I don't know if this is your problem, but OnSquare->getID should probably be OnSquare->getID() Commented Nov 26, 2011 at 11:59

2 Answers 2

6

You have to make sure that the class definition of Square is visible when you use the class (by dereferencing the pointer). Typically like this:

// player.hpp

class Square;

class Player
{
  void captureSquare(Square* []);
};

// player.cpp

#include "player.hpp"
#include "square.hpp"

void Player::captureSquare(Square* allSquares[22])
{
  allSquares[0]->foo();  // need complete class here!
}
Sign up to request clarification or add additional context in comments.

Comments

1

An error complaining about an incomplete type usually means that the class's definition is not available at that point. Do you perhaps have a line like class Square; (without the usual curly brackets and their contents) somewhere in the same file or in an included file?

It's hard to say anything more definite without seeing more of your code (and without knowing which line the compiler is complaining about).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.