• Advertisement
Sign in to follow this  

Why am I getting error C2146 when trying to declare a class object.

This topic is 3034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#pragma once
#include "Text.h"
#include <d3d9.h>
#include <d3dx9.h>

class Graphics
{
	public:
		Graphics();
		~Graphics();
		
		// Initialize all graphics 
		HRESULT InitalizeGraphics( HWND hWnd );
		
		// Render screen
		HRESULT BeginScene();
		HRESULT EndScene();
		HRESULT ClearDisplay();
		HRESULT Present();
		void Render();

		// Clean up graphics
		void ShutdownGraphics();

		//LPDIRECT3DDEVICE9& Get3DDevice(){return m_device.Get3DDevice();};

	private:
                // Issue with this
		//Text m_textManager;		// Text object

};


#pragma once
#include <string>
#include <list>
#include "Graphics.h"
#include <d3d9.h>
#include <d3dx9.h>

class Text
{
	public:
		Text();
		~Text();

		int CreateFont(std::string &fontName);

	private:
                // Why can't I declare a Graphics object. 
		//Graphics  m_graphicsReference; //error C2146 sytax error : missing ";" befire indentifier
		std::list< LPD3DXFONT > m_fonts; 
		LPD3DXFONT	m_font;

		int m_fontUID;

		int GetFontUID();
};

error C2146: syntax error : missing ';' before identifier When I declare Graphics object inside of Text I get the error. When I declare a Text object inside of Graphics and don't decalre a Graphics object inside of Text it work. When I declare a Text object inside of Graphics and I decalre a Graphics object inside of Text I get the error. Some further info - Graphics is also declared by another class. Any suggestions Regards Chad

Share this post


Link to post
Share on other sites
Advertisement
You can't have a member of Graphics with type Text and a member of Text with type Graphics. Think about it this way: what will take more space in memory, a Text or a Graphics?

You can however have a member of Graphics which is a pointer to Text and a member of Text which is a pointer to Graphics. For that you need to use a forward declaration.

Share this post


Link to post
Share on other sites
So if I am gathering things correctly..

class A
{
b(this);
}

class B
{
A *a;
B(const *Aref)
: a(Aref)
{ }
}

This unfortunetly does not work. I am still getting the error of not being able to identify A in b or visa versa.

Regards

Chad

Share this post


Link to post
Share on other sites
Does this seem correct
#pragma once
class Graphics;

class Text
{
public:
Text(const Graphics &graphicsReference);
~Text();

int CreateFont(std::string &fontName);

private:
const Graphics &m_graphicsReference;
std::list< LPD3DXFONT > m_fonts;
LPD3DXFONT m_font;

int m_fontUID;

int GetFontUID();
};

Text::Text(const Graphics &graphicsReference)
: m_graphicsReference(graphicsReference)
{
m_font = NULL;
m_fontUID = 0;
}



Can you actually does this with a reference type or does it have to be a pointer.


Regards

Chad

Share this post


Link to post
Share on other sites
A reference is fine too. As long as you've broken the circular dependency/membership, which you have.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement