Sign in to follow this  
Nuklear

returning pointer to customvertex buffer

Recommended Posts

What iI'm trying to achieve is to create a class that will create its own vertex buffer for the object it describes (a square in this case), then to return pointer to that buffer so I can memcpy it to my main vertex buffer and then render it. This is my block.h:
/* Defines a single block */
class cBlock
{
  public:
  // Our basic functions for initialization/drawing/destroying
  void Create();
  void Destroy();

  // Constructor & Destructor
  //cBlock();
  //virtual ~cBlock();

  // Get/Set procedures for block position
  int GetPosX();
  int GetPosY();
  CUSTOMVERTEX &vertbuffer();

  void SetPosX(int x);
  void SetPosY(int y);

  private:
  // Block position on the game’ grid
  int m_iPosX, m_iPosY;
  RECT rBlock;
};
And my block.cpp:
int cBlock::GetPosX()
{
	return m_iPosX;
}

int cBlock::GetPosY()
{
	return m_iPosY;
}

int * cBlock::vertbuffer()
{
	// Calculate block rect using pre-defined variables
	rBlock.left = m_iPosX * (BLOCK_SIZE + BLOCK_SPACING) + BLOCK_SPACING;
	rBlock.right = rBlock.left + BLOCK_SIZE - BLOCK_SPACING;
	rBlock.bottom = m_iPosY * (BLOCK_SIZE + BLOCK_SPACING) - BLOCK_SPACING;
	rBlock.top = rBlock.bottom - BLOCK_SIZE + BLOCK_SPACING;
	
	//Fill out vertex buffer for block
	CUSTOMVERTEX vertbuffer[4] =
	{
		{rBlock.left, rBlock.bottom, 5.0f, 1.0f, float(D3DCOLOR_XRGB(255, 255, 255))},
		{rBlock.left, rBlock.top, 5.0f, 1.0f, float(D3DCOLOR_XRGB(255, 255, 255))},
		{rBlock.right, rBlock.bottom, 5.0f, 1.0f, float(D3DCOLOR_XRGB(255, 255, 255))},
		{rBlock.right, rBlock.top, 5.0f, 1.0f, float(D3DCOLOR_XRGB(255, 255, 255))},
	};
	
	//Return pointer to vertex buffer
	CUSTOMVERTEX * pVertbuffer = &vertbuffer[0];
	return *pVertbuffer;
}

void cBlock::SetPosX(int x)
{
	m_iPosX = x;
}

void cBlock::SetPosY(int y)
{
	m_iPosY = y;
}
However when I try to compile it (VC++ 2005) i get the following errors:
e:\documents and settings\adnan\my documents\visual studio 2005\projects\tetris\tetris\block.h(38) : error C2556: 'int *cBlock::vertbuffer(void)' : overloaded function differs only by return type from 'CUSTOMVERTEX &cBlock::vertbuffer(void)'
        e:\documents and settings\adnan\my documents\visual studio 2005\projects\tetris\tetris\block.h(16) : see declaration of 'cBlock::vertbuffer'
e:\documents and settings\adnan\my documents\visual studio 2005\projects\tetris\tetris\block.h(38) : error C2372: 'cBlock::vertbuffer' : redefinition; different types of indirection
        e:\documents and settings\adnan\my documents\visual studio 2005\projects\tetris\tetris\block.h(16) : see declaration of 'cBlock::vertbuffer'
Any suggestions of where I'm going wrong?

Share this post


Link to post
Share on other sites
In your class definition you define the vertbuffer()-function to return a reference to CUSTOMVERTEX and in your implementation you define the vertbuffer function to return an pointer to int.
In C++ it's not allowed to overload functions by return-type (only), so the compiler sees your different function-prototypes and causes this error.
To solve this you have to change the return-type to be the same in your class definition and your function implementation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this