Archived

This topic is now archived and is closed to further replies.

Jacob84

Desperate - IDE problems or just stupid developer?

Recommended Posts

Hi everybody: Please, let''s see if somebody can help me, because I''m really sad . I''m developing a 2D strategy game, directly with the MFC''s. In the graphic aspect it''s so simple. I have now almost 600 lines of code, and a bit complicated classes diagram. I have two years C++ experience only, but just a little in windows''s programming. What I''m trying to make, and where I''m stucked, is in creating a CAllocator class , that keeps updated pointers to the most important objects in the program. One instance of this class will be global. Let''s see the problem: This is the Alocator.h #pragma once #include "base.h" class CAllocator { public: CAllocator() { c = NULL; } CCuad *c; }; extern CAllocator *global; In base.h there is included the "CCuad.h" , and all .h files have #pragma once on the top. But when I try to compile I get messages , saying that the compiler can''t access to CCuad object. I can''t understand it... In the main file, I have also the CAllocator *global pointer (wich is extern) and global = new CAllocator(); I get also "extrange" messages. Please, somebody help me !!! :D I don''t know what to do... ?J@cob

Share this post


Link to post
Share on other sites
Better avoiding global instances, as it is a bad style of programming.

What i suggest is to make your class a singleton.

To do it simple, try something like that

class CAllocator
{
protected:
CAllocator () //Users cannot create instances
{
m_pAllocatorInstance = null;
}

public:
CAllocator *GetInstance ()
{
if ( ! m_pAllocatorInstance )
{
m_pAllocatorInstance = new CAllocator();

//TODO: Allocate your variables as a constructor
m_pQuad = new CCuad();

}

return m_pAllocatorInstance;
}

private:
CAllocator *m_pAllocatorInstance;
CCuand *m_pCuad;
};


This is the simplest way of creating a singleton, and it guarantee that you''ll have only one instance of your class.

Espero que haya sido util.

Cybird

Share this post


Link to post
Share on other sites
First, thanks Cybird for posting ... really appreciated.

This way of create singleton classes is very interesting. But I still have problems, and I'm starting to think, that they could be product of some rare and non-related programation topic, because I can't find some good explanation.

I have a class CWindow, as you probably know if you have used MFC's it "derives" from CFrameWnd. This class is over two files, Window.cpp & Window.h, and it seems to work ok, and seems that don't have any visible problem. Let's move now over the Allocator.h, wich code is at the bottom. If I remove the comments over CWindow related lines, I get 3 errors, that seems to say that compiler can't find CWindow implementation. How is this possible??? I have included "CWindow.h" on the top of the file...


#include "Window.h"

class CAllocator
{
protected:
CAllocator () { m_pAllocatorInstance = NULL; }
public:
CAllocator *GetInstance()
{
if ( ! m_pAllocatorInstance )
{
m_pAllocatorInstance = new CAllocator();
//m_v = new CWindow();
}
return m_pAllocatorInstance;
}
private:
CAllocator *m_pAllocatorInstance;
//CWindow *m_v;
};

Thanks a lot ... I hope to solve this big problem soon


?J@cob

[edited by - Jacob84 on January 17, 2004 10:35:30 AM]

[edited by - Jacob84 on January 17, 2004 10:36:25 AM]

Share this post


Link to post
Share on other sites
It's not saying that you didn't include declarations or definitions when it asks for an implementation to CWindow. Basically, the compiler wants you to use CWindow in your Allocator.h file. Without those lines that you have commented(Which I assume are not commented in your real code) CWindow is not used in Allocator.h

Edited for grammar mistake

----------------------------------
"There are 10 kinds of people in this world, those who know binary, and those who don't"

[edited by - 4TheKing on January 17, 2004 10:54:55 AM]

Share this post


Link to post
Share on other sites
Ey 4TheKing Thanks for reply...

I don''t think so (But I''m not sure...). The errors I get, are parse errors (Or that''s what I think).

VS.net errors code C2143, C2501, C2501. They are like:

Must be a '';'' before * here : CWindow *m_v; or there is no type specifier. As far as I know, I think is related to other think. Can the #pragma precompiler keyword affect in some way? (I use #pragma once, but #ifndef,#define,#endif was used and there wasn''t success ... I think).

Thanks guys

?J@cob

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"atlwin.h" has the CWindow class definition in it; I''m not sure that you''re ment to #include this directly ... OTOH I don''t know what should be included directly for this; Try it & see what happens, it might be okay (the ''suck-it-and-see'' principle!)

[btw: if you need to find class definitions, try using the windows search feature in the mfc/atl include directory with something like "class CWindow"; alternatively in the MSDN docs find the class & at the bottom of the page it usually tells you which files you need to #include and to add as libraries]

Share this post


Link to post
Share on other sites
Hi everybody!

I think I'm not explaining the problem correctly. CWindow is a derived class (I don't know if "derived" is the word, sorry ) from CFrameWnd.(CWindow is not included in ATL or MFC, just mine)

But the real problem, doesn't occur with CWindow only. The BIG problem , is that, I can't use any class that I have created in other (.h & .cpp) files. If I create a new class CTest ( Test.h & Test.cpp ) , and I add it to the private part like that:

CTest *t;

I get errors in compilation. How is this possible? I'm missing something? All .h files have his #pragma once at the first line of code. Thanks!

PS: Obviously, #include "Test.h" is placed in "Allocator.h" file. I don't know what is going on!!!

?J@cob

[edited by - Jacob84 on January 17, 2004 12:27:01 PM]

Share this post


Link to post
Share on other sites