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

Started by
3 comments, last by mattd 14 years, 5 months ago
#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
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.

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
Does this seem correct
#pragma onceclass 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
A reference is fine too. As long as you've broken the circular dependency/membership, which you have.

This topic is closed to new replies.

Advertisement