Home » Community » Forums » General Programming » Why am I getting error C2146 when trying to declare a class object.
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Why am I getting error C2146 when trying to declare a class object.
Post New Topic  Post Reply 
#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

 User Rating: 1029   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.



 User Rating: 1554   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1029   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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

 User Rating: 1029   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

A reference is fine too. As long as you've broken the circular dependency/membership, which you have.

 User Rating: 1611   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: