Sign in to follow this  

Declaring a static class and accessing it

This topic is 4338 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

G'day, I've made some form classes for a program I'm making: LoginWindow - Class for the login window OptionsWindow - Class for the options window Etc... These classes have constructors and decontructors and functions like "ShowLoginWindow" for example to display the window, etc. My problem is that I want to declare these classes in the Main.h file which is the main header file for Main.cpp which has the WinMain function. Currently I have included the class header files in Main.cpp and have declared the classes and shown the login screen: LoginWindow Login; Login.CreateLoginWindow(); I was wondering how I could declare a universal class as I only want one instance of each class. Basically I want to know if its possible (or worth) declaring the classes as a static in the Main.h, this way other classes can then access the only instance of each other class. Follow? So in the Login window, there might be a button that says Options. Instead of creating a new form everytime they click the Options button to go to options, I just want it to keep what has already been created. [Main.h] static LoginWindow Login; static OptionsWindow Options; This returns an error: error C2146: syntax error : missing ';' before identifier 'Login' error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C4430: missing type specifier - int assume ...and some more errors to do with the OptionsWindow class. I'm guessing I'm either doing this wrong or it can't be done, or it's not supposed to be done. A little help thanks :P

Share this post


Link to post
Share on other sites
Hi bud,

If i understand you correctly then congrats on getting something working in WIN32, it's a nightmare.

Regarding your question, i wouldn't bother with trying to share a window object between several classes. Just 'new' a new one whenever you need to. Atleast for now anyways, get your program working before you try to make it work better.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
Hi bud,

If i understand you correctly then congrats on getting something working in WIN32, it's a nightmare.

Regarding your question, i wouldn't bother with trying to share a window object between several classes. Just 'new' a new one whenever you need to. Atleast for now anyways, get your program working before you try to make it work better.

Hope that helps,

Dave


Haha, yeah Win32 is a pain. Thanks for the answer, but the way my program is designed needs to have a static class, or something to that effect.

Share this post


Link to post
Share on other sites
I tried that. Seemed like it would work until it returned the same error of:

error C2146: syntax error : missing ';' before identifier 'g_Login'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assume

Share this post


Link to post
Share on other sites
This is my current code in those modules:

Main.h

#ifndef __MAIN_H
#define __MAIN_H

#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "wsock32.lib")

#define WIN32_LEAN_AND_MEAN
//#define ISOLATION_AWARE_ENABLED 1

#include <windows.h>
#include <commctrl.h>
#include <shellapi.h>
#include <stdio.h>
#include <malloc.h>
#include "login.h"

#define WM_SOCKET_ASYNC WM_USER + 100

static bool bConnected = false; // Connected... or not =P

void CenterOnScreen(HWND hWnd, RECT Client);
bool IsFileExist(LPSTR lpszFileName);

LoginWindow g_Login;
static LoginWindow* g_pLogin = &g_Login;

#endif



Login.h

#ifndef __LOGIN_H
#define __LOGIN_H

#include "main.h"

class LoginWindow
{
public:
bool CreateLoginWindow();
void DestroyLoginWindow();
HWND GetWindow() const;
bool IsCreated() const;
};

#endif

Share this post


Link to post
Share on other sites
I think I have it working now. I'm still experimenting. I have create a new header file with all the statics. To avoid an include loopback error, I have referenced the new header to all the class header files and main.h. each class header refers to main.h as well. I'm still fiddling with it but so far it's looking promising and I have created some windows already so there's no compile errors... yet. Thanks for all your help so far guys!

Share this post


Link to post
Share on other sites

This topic is 4338 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this