关于Static变量的问题!!!
我在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?请问在类的静态成员函数里能否使用非静态的成员变量?
致
礼!