Sign in to follow this  
crazy_andy

CString assighnment problem

Recommended Posts

I have a class defined in global.h called globals, it contains a static variable called DLdirectory. I have another class RegFunc defined in Registry.h + .cpp, I include global.h and I at the beginning of the cpp file I have CString globals::DLDirectory (If I don't I get linker errors even though I can acces it from other cp files without that at the beginning) I also have CString globals::DLDirectory at the top of my main cpp file. Anyway, I have a char data[1024] containg "c:\documents .... " (replace dots with the rest of the path) but globals::DLdirectory = data; gives DLDirectory having the value "" globals::DLdirectory.Format(_T("%c"),data); also gives the vlue "" however in other cpp files globals::DLdirectory = data works great. Any ideas on whats going on here?

Share this post


Link to post
Share on other sites
Well, it's doing that because you have another instance of DLDirectory. I'm actually surprised it doesn't give you a redefinition error.

Find out why it gives you a linker error in the beginning and go from there.

Share this post


Link to post
Share on other sites
Quote:
Original post by Maega
Well, it's doing that because you have another instance of DLDirectory. I'm actually surprised it doesn't give you a redefinition error.

Find out why it gives you a linker error in the beginning and go from there.


Does he have another instance? Looks to me like he might be initializing the static DLDirectory member in multiple files, though..

Some source code might help.

Share this post


Link to post
Share on other sites
I am initialising it in two files, but without the 2nd definition in registry.cpp I get compiler errors. heres some code

be warned, its messsy, but I'm improving it as I go through.

registry.cpp (only relevant part)

#include "registry.h"
#include "global.h"

CString globals::DLDirectory; // without these lines I get the linker error
// error LNK2001: unresolved external symbol "public: static class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > globals::DLDirectory" (?DLDirectory@globals@@2V?$CStringT@_WV?$StrTraitATL@_WV?$ChTraitsCRT@_W@ATL@@@ATL@@@ATL@@A)
CString globals::TempDirectory;
CString globals::EDFIDirectory;

void RegFuncs::ReadFromRegistry()
{
char data[1024];
memset(data,0,1024);
DWORD size = 1024;

if(RegQueryValueEx(globals::hk,_T("Download Folder\0"),NULL,NULL,(LPBYTE)data,&size))
{
MessageBox(NULL,_T("The registry values have been corrupted. Please reset your options."),_T("Error"),MB_OK|MB_ICONSTOP);
CreateNewKeys();
return;
}
for (int i = 0;i<1024;i+=2)
{
data[i/2] = data[i];
}
globals::DLDirectory = data;
}


registry.h

#include <windows.h>

#if !defined REGISTRY_H
#define REGISTRY_H

class RegFuncs
{
public:
void CheckRegistry();
void ReadFromRegistry();
void CreateNewKeys();
void WriteNewKeys();
};

#endif


maincode.cpp (oddly enough the entry point) (cut down again)

#include <afxwin.h>
#include <afxcmn.h>
#include "resource.h"
#include "download.h"
#include "Dialog.h"
#include "DirectoryTree.h"
#include "global.h"
#include "registry.h"

int globals::current = 0;
char* globals::URL[1];
bool globals::DLRequested[1];

bool globals::FolderChanged;
CString globals::Folder;
CString globals::DLDirectory;
CString globals::TempDirectory;
CString globals::EDFIDirectory;
CString globals::NewFolder;
bool globals::IsNewFolder;
DWORD globals::Alarm;
DWORD globals::AutoCreate;
DWORD globals::Baloon;
DWORD globals::ExitComplete;
DWORD globals::Minimize;
HKEY globals::hk;

class MainCode :public CFrameWnd
{
public:

CMenu menu;
download files[1];

MainCode()
{
RegFuncs reg;
reg.CheckRegistry();
Create(NULL,_T("Easy Downloader - Version 0.1"));
menu.LoadMenu(IDR_MENU1);
SetMenu(&menu);
}

void CreateNewDownload();
void OnOptions();

DECLARE_MESSAGE_MAP()
};


global.h

#include <atlstr.h>
#if !defined GLOBAL_H
#define GLOBAL_H

class globals
{
public:
static char *URL[1];
static int current;
static bool DLRequested[1];

static bool FolderChanged;
static CString Folder;

static CString DLDirectory;
static CString TempDirectory;
static CString EDFIDirectory;

static CString NewFolder;
static bool IsNewFolder;

static DWORD AutoCreate;
static DWORD Alarm;
static DWORD Baloon;
static DWORD ExitComplete;
static DWORD Minimize;

static HKEY hk;
};

#endif


I have more code, as you can see, but its not relevant

Share this post


Link to post
Share on other sites
ok whats extern?
and I havn't worked any other way out to have global variable accesable in all files. I'm probably pretty stuppid. my two main coding problems are:

1) data cnvertions
2) header + source files.

oh and as I progress in the project, there will be a lot more global variables. because I need to pass data to and from classes using CDialog::DoModal()

Share this post


Link to post
Share on other sites

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