|
||||||||||||||||||
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. |
|
![]() chadsxe Member since: 1/6/2006 From: Chicago, IL, United States |
||||
|
|
||||
#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(); }; 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 |
||||
|
||||
![]() alvaro Member since: 3/7/2002 From: USA |
||||
|
|
||||
| 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. |
||||
|
||||
![]() chadsxe Member since: 1/6/2006 From: Chicago, IL, United States |
||||
|
|
||||
So if I am gathering things correctly..class A { b(this); } class B { A *a; B(const *Aref) : a(Aref) { } } Regards Chad |
||||
|
||||
![]() chadsxe Member since: 1/6/2006 From: Chicago, IL, United States |
||||
|
|
||||
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 |
||||
|
||||
![]() mattd Member since: 2/26/2000 |
||||
|
|
||||
| A reference is fine too. As long as you've broken the circular dependency/membership, which you have. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|