Jump to content
  • Advertisement
Sign in to follow this  
crazy_andy

Classes and variable.

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

I have just started with MFC, and I have a cpp file with two classes in, one myapp, and the other to create the window. Also I have a couple of global variables and a couple of functions to deal with calculations, which arnt a part of either class. I also have another cpp file and a header file, containg a class for a dialog box. I use a function in there to extract data from a combo box when A button is pressedd. This data is stored as an int. I want to be able to access this variable from anyother function not necessary class. At the moment from one of the functtions that arn't in a class. Now I have tried declaring the variable as public, private, static, in the header, in both cpp files but I keep on getting errors. Either: 1) illegal reference to non-static member 'newDialog::hRemain When I declare it as public in the header. 2) 'hRemain' : undeclared identifier When I declare it as globals at the top of the main cpp file 3) code.obj : error LNK2001: unresolved external symbol "public: static int newDialog::sRemain" (?sRemain@newDialog@@2HA) newDialog.obj : error LNK2001: unresolved external symbol "public: static int newDialog::sRemain" (?sRemain@newDialog@@2HA) code.obj : error LNK2001: unresolved external symbol "public: static int newDialog::mRemain" (?mRemain@newDialog@@2HA) newDialog.obj : error LNK2001: unresolved external symbol "public: static int newDialog::mRemain" (?mRemain@newDialog@@2HA) code.obj : error LNK2001: unresolved external symbol "public: static int newDialog::hRemain" (?hRemain@newDialog@@2HA) newDialog.obj : error LNK2001: unresolved external symbol "public: static int newDialog::hRemain" (?hRemain@newDialog@@2HA) Debug/Countdown.exe : fatal error LNK1120: 3 unresolved externals I get that when I declare them as static in the header file. 4) I can't remember exactly how I got this but I got hRemain allready defined in code.obj I've run out of ideas now. Can any one help and give me a suggestion of where I can declare them. It would be greatly appreciated

Share this post


Link to post
Share on other sites
Advertisement
here is my code

This is the main code

#include <afxwin.h>
#include <afxext.h>
#include "resource.h"
#include "newDialog.h"
#include<sys\timeb.h>
#include<time.h>

void GetTime(bool,int &,int &,int &);

bool finish = false;
bool activated = false;
bool newTime = false;

/*int hRemain;
int mRemain;
int sRemain;*/

UINT TimingThread(LPVOID pParam)
{
int hFin = 0, mFin = 0, sFin = 0;
int hCur = 0, mCur = 0, sCur = 0;
while(!finish)
{
Sleep(1000);
if (activated)
{
GetTime(true,hFin,mFin,sFin);
}
else if(newTime)
{
GetTime(false,hFin,mFin,sFin);
GetTime(true,hCur,mCur,sCur);
}
}


return 1;
}
//******************************* here is my function trying to get the values
//**************************************************************

void GetTime(bool tag,int &hT, int &mT, int &sT)
{
struct _timeb timebuff;
char *timeline;
_ftime(&timebuff);
timeline = ctime(&(timebuff.time)); // returns "Fri Feb 04 16:47:27 2005"

if (tag)
{
hT = ((timeline[11]-48)*10)+(timeline[12]-48)+newDialog::hRemain; //<----

mT = ((timeline[14]-48)*10)+(timeline[15]-48)+newDialog::mRemain; // <-----

sT = ((timeline[17]-48)*10)+(timeline[18]-48)+newDialog::sRemain; // <-------

if (sT > 59)
{
mT++;
sT -= 60;
}

if (mT > 60)
{
hT++;
mT -= 60;
}

if (hT > 24)
{
hT -= 24;
}
}
else
{
hT = ((timeline[11]-48)*10)+(timeline[12]-48);

mT = ((timeline[14]-48)*10)+(timeline[15]-48);

sT = ((timeline[17]-48)*10)+(timeline[18]-48);
}
}


class MFC_Tutorial_Window :public CFrameWnd
{
CMenu menu1; // dedines my menu
public:
HANDLE tThread;
MFC_Tutorial_Window()
{
Create(NULL,"MFC Tutorial Part 5 CoderSource Dialog"); // creates window

menu1.LoadMenu(IDR_MENU1); // sets up menu
SetMenu(&menu1);
}


void OnClickNew(); // defines function for when the new button is clicked
int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP() // sets up a message map, for message handling
};

BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd) // begining of message map
ON_WM_CREATE()
ON_COMMAND(ID_FILE_NEW,OnClickNew) // when new is clicked do function OnClickNew()
END_MESSAGE_MAP() // end of message map

void MFC_Tutorial_Window::OnClickNew() // what to do when new is clicked
{
newDialog dlg; //defines dialog box
dlg.DoModal(); //creates a dialog box
}

int MFC_Tutorial_Window::OnCreate(LPCREATESTRUCT lpCreateStruct) //creates toolbar
{
MessageBox("t");
tThread = AfxBeginThread(TimingThread,0,0,0,0);
return 1;
}

class MyApp :public CWinApp
{
MFC_Tutorial_Window *wnd;
public:
BOOL InitInstance()
{
wnd = new MFC_Tutorial_Window();
m_pMainWnd = wnd;
m_pMainWnd->ShowWindow(1);
return 1;
}
};

MyApp theApp;



here is the newDialog header

#include<afxwin.h>
#include "afxwin.h"

#if !defined(AFX_MYDIALOG_H__20B59A5E_FBE6_4A1C_A6B7_FDC199FE74EC__INCLUDED_)
#define AFX_MYDIALOG_H__20B59A5E_FBE6_4A1C_A6B7_FDC199FE74EC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class newDialog: public CDialog
{
public:
newDialog(CWnd* pParent = NULL);

enum { IDD = IDD_NEW };

afx_msg void OnBnClickedAddTime();
CComboBox NumHoursSel;
CComboBox NumMinsSel;
CComboBox NumSecsSel;

int hRemain; //<-variables currently declared
int mRemain;
int sRemain;

protected:
virtual void DoDataExchange(CDataExchange* pDX);
DECLARE_MESSAGE_MAP()
};

#endif


Here is the cpp file for the dialog

#include "resource.h"
#include "newDialog.h"
#include ".\newdialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

newDialog::newDialog(CWnd* pParent)
: CDialog(newDialog::IDD, pParent)
{
}

void newDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO1, NumHoursSel);
DDX_Control(pDX, IDC_COMBO2, NumMinsSel);
DDX_Control(pDX, IDC_COMBO3, NumSecsSel);
}

BEGIN_MESSAGE_MAP(newDialog,CDialog)

ON_BN_CLICKED(ID_ADD_TIME, OnBnClickedAddTime)
END_MESSAGE_MAP()


void newDialog::OnBnClickedAddTime() <- function to retrieve fata from combo boxes
{
UpdateData();
CString hTime, mTime, sTime;
int index;

hRemain = 0; < <---- problem variables
mRemain = 0;
sRemain = 0;
bool error = false;

index = NumHoursSel.GetCurSel();
if (index != -1)
{
NumHoursSel.GetLBText(index,hTime);
hRemain = atoi(hTime); <---- set here
}
else
{
error = true;
MessageBox("Hours must be between 0 and 23","Invalid Time");
}

index = NumMinsSel.GetCurSel();
if (index != -1)
{
NumMinsSel.GetLBText(index,mTime);
mRemain = atoi(mTime);
}
else
{
error = true;
MessageBox("Minutes must be between 0 and 59","Invalid Time");
}
index = NumSecsSel.GetCurSel();
if (index != -1)
{
NumSecsSel.GetLBText(index,sTime);
sRemain = atoi(sTime);
}
else
{
error = true;
MessageBox("Secconds must be between 0 and 59","Invalid Time");
}

UpdateData(false);


if (hRemain == 0 && mRemain == 0 && sRemain == 0 && !error)
{
MessageBox("Time can not be 0","Invalid Time");
}
else if (error)
{
}
else if (hRemain > 23 || mRemain > 59 || sRemain > 59)
{
MessageBox("The time you have entered is too long. The maximum length is 23:59:59","Invalid Time");
}
else
{
MessageBox("Timer has started.","Timer started");
EndDialog(1);
}

}


Basically It recieves a time from the dialog boxes. Then it adds that time to the current time. Which is done in the thread created in the main cpp file.

The code like this gives me the errors

c:\Documents and Settings\Andrew\My Documents\Visual Studio Projects\Countdown\code.cpp(49) : error C2597: illegal reference to non-static member 'newDialog::hRemain'
c:\Documents and Settings\Andrew\My Documents\Visual Studio Projects\Countdown\code.cpp(49) : error C2568: '+' : unable to resolve function overload
unable to recover from previous error(s); stopping compilation

Share this post


Link to post
Share on other sites
sorry about the long post ^^

I used [code ] instead of source It won't let me edit it either

For the same code, but with the variables declared as

static int hRemain;
static int mRemain;
static int sRemain;

I get these linker errors

Linking...
code.obj : error LNK2001: unresolved external symbol "public: static int newDialog::sRemain" (?sRemain@newDialog@@2HA)
newDialog.obj : error LNK2001: unresolved external symbol "public: static int newDialog::sRemain" (?sRemain@newDialog@@2HA)
code.obj : error LNK2001: unresolved external symbol "public: static int newDialog::mRemain" (?mRemain@newDialog@@2HA)
newDialog.obj : error LNK2001: unresolved external symbol "public: static int newDialog::mRemain" (?mRemain@newDialog@@2HA)
code.obj : error LNK2001: unresolved external symbol "public: static int newDialog::hRemain" (?hRemain@newDialog@@2HA)
newDialog.obj : error LNK2001: unresolved external symbol "public: static int newDialog::hRemain" (?hRemain@newDialog@@2HA)
Debug/Countdown.exe : fatal error LNK1120: 3 unresolved externals


Share this post


Link to post
Share on other sites
Ahaha, that fixed it. I thought when you said that first you meant in the header file. I assumed that anywhere else It would cause a redifined error. Thanks alot.

Share this post


Link to post
Share on other sites
Hi,

I am also facing a similar issue. Where did you add the int newDlg::variable = 0 ?

I am getting redeclaraion error.

thanks.!

Share this post


Link to post
Share on other sites
Hi,

I am also facing a similar issue. Where did you add the int newDlg::variable = 0 ? I mean, which source file did you add this to ?

I am getting redeclaraion error.

thanks.!

Share this post


Link to post
Share on other sites
Hi,

I am also facing a similar issue. Where did you add the int newDlg::variable = 0 ? I mean, which source file did you add this to ?

I am getting redeclaraion error.

thanks.!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!