关于Static变量的问题!!!

codesphere 2005-10-13 02:50:55
我在VC6的一个类里加了一个STATIC变量,又加了一个STATIC方法,在方法里引用这个STATIC变量,但是链接的时候却报错说什么“error LNK2001: unresolved external symbol "protected: static class CString CMyTestDlg::s" (?s@CMyTestDlg@@1VCString@@A)
Release/MyTest.exe : fatal error LNK1120: 1 unresolved externals”

代码如下:
.h文件
class CMyTestDlg : public CDialog
{
// Construction
public:
static void TestFunc();
CMyTestDlg(CWnd* pParent = NULL); // standard constructor


// Dialog Data
//{{AFX_DATA(CMyTestDlg)
enum { IDD = IDD_MYTEST_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyTestDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;
static CString s;
// Generated message map functions
//{{AFX_MSG(CMyTestDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
.cpp文件
CMyTestDlg::CMyTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyTestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyTestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyTestDlg, CDialog)
//{{AFX_MSG_MAP(CMyTestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTestDlg message handlers

BOOL CMyTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CMyTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CMyTestDlg::TestFunc()
{
s="abc";

}
真是真了鬼了,难道是VC的一个BUG?请问在类的静态成员函数里能否使用非静态的成员变量?

        致
礼!
...全文
163 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
2c18 2005-10-13
  • 打赏
  • 举报
回复
类中的静态成员变量的初始化语句中必须要声明它的类型,又因为静态成员函数没有this指针所以要在静态成员函数中对静态变量加类别限定符::,综上所述,用以下形式描述:
void CMyTestDlg::TestFunc()
{
CString CMyTestDlg::s = "abc";
}
至于在CString前面加不加static无所谓
fyx010641 2005-10-13
  • 打赏
  • 举报
回复
.cpp文件
static CString s; /*申明一下*/
CMyTestDlg::CMyTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyTestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyTestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyTestDlg, CDialog)
//{{AFX_MSG_MAP(CMyTestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTestDlg message handlers

BOOL CMyTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CMyTestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CMyTestDlg::TestFunc()
{
this->s="abc";

}
fyx010641 2005-10-13
  • 打赏
  • 举报
回复
可以的呀!
Static是要初始化的,在.CPP的外部!!
hurricane511 2005-10-13
  • 打赏
  • 举报
回复
不用声明为static也可以呀,直接在cpp中加入CStringCMyTestDlg::s;就可以了!
wuchi 2005-10-13
  • 打赏
  • 举报
回复
re,静态变量是放不同地址存储的,要再声明一下
老夏Max 2005-10-13
  • 打赏
  • 举报
回复
鸟人说的正确:static CString CMyTestDlg::s;
可以赋初始值。
因为是类的静态成员所以可以定义为public的。
快乐鹦鹉 2005-10-13
  • 打赏
  • 举报
回复
在cpp文件中,要增加 static CString CMyTestDlg::s;

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧