我用MFC建了个新类CBAR,里面定义了如下静态变量(公有成员变量):
static HBRUSH hbrush;
static int l1;
static RECT re;
static int l;
static CDC *dc;
类外赋过初值了,并在类CBAR的成员函数中改变了这些变量的值.
我在另一个类CTEST1DLG中想引用这些变量,引用方法如下:
extern HBRUSH hbrush;
extern int l1;
extern RECT re;
extern int l;
extern CDC *dc;
但出现了以下错误:
BAR.obj : error LNK2001: unresolved external symbol "public: static int CBAR::l1" (?l1@CBAR@@2HA)
BAR.obj : error LNK2001: unresolved external symbol "public: static int CBAR::l" (?l@CBAR@@2HA)
BAR.obj : error LNK2001: unresolved external symbol "public: static struct HBRUSH__ * CBAR::hbrush" (?hbrush@CBAR@@2PAUHBRUSH__@@A)
BAR.obj : error LNK2001: unresolved external symbol "public: static struct tagRECT CBAR::re" (?re@CBAR@@2UtagRECT@@A)
BAR.obj : error LNK2001: unresolved external symbol "public: static class CDC * CBAR::dc" (?dc@CBAR@@2PAVCDC@@A)
Debug/Test1.exe : fatal error LNK1120: 5 unresolved externals
请问各位高手这个错误怎么改?工程已经包括了BAR.h和BAR.cpp文件.
另外还有个问题:我这么引用这些类CBAR中的静态变量,主要是想在类CTEST1DLG中引用这些变量改变后的值,请问这个目的能达到吗?
谢谢各位高手啊
static HBRUSH hbrush;
static int l1;
static RECT re;
static int l;
static CDC *dc;
类外赋过初值了,并在类CBAR的成员函数中改变了这些变量的值.
我在另一个类CTEST1DLG中想引用这些变量,引用方法如下:
extern HBRUSH hbrush;
extern int l1;
extern RECT re;
extern int l;
extern CDC *dc;
但出现了以下错误:
BAR.obj : error LNK2001: unresolved external symbol "public: static int CBAR::l1" (?l1@CBAR@@2HA)
BAR.obj : error LNK2001: unresolved external symbol "public: static int CBAR::l" (?l@CBAR@@2HA)
BAR.obj : error LNK2001: unresolved external symbol "public: static struct HBRUSH__ * CBAR::hbrush" (?hbrush@CBAR@@2PAUHBRUSH__@@A)
BAR.obj : error LNK2001: unresolved external symbol "public: static struct tagRECT CBAR::re" (?re@CBAR@@2UtagRECT@@A)
BAR.obj : error LNK2001: unresolved external symbol "public: static class CDC * CBAR::dc" (?dc@CBAR@@2PAVCDC@@A)
Debug/Test1.exe : fatal error LNK1120: 5 unresolved externals
请问各位高手这个错误怎么改?工程已经包括了BAR.h和BAR.cpp文件.
另外还有个问题:我这么引用这些类CBAR中的静态变量,主要是想在类CTEST1DLG中引用这些变量改变后的值,请问这个目的能达到吗?
谢谢各位高手啊
CBAR::l1;
CBAR::re;
CBAR::l;
CBAR::dc;
extern HBRUSH hbrush;
extern int l1;
extern RECT re;
extern int l;
extern CDC *dc;
=====================
你这样引用的话,编译认为这是全局变量,而不认为是类的成员变量.
extern HBRUSH CBAR::hbrush;
extern int CBAR::l1;
extern RECT CBAR::re;
extern int CBAR::l;
extern CDC CBAR::dc;
但出现了以下错误:
d:\调试文件夹\test1\test1dlg.h(12) : error C2720: 'hbrush' : 'extern ' storage-class specifier illegal on members
d:\调试文件夹\test1\test1dlg.h(13) : error C2720: 'l1' : 'extern ' storage-class specifier illegal on members
d:\调试文件夹\test1\test1dlg.h(14) : error C2720: 're' : 'extern ' storage-class specifier illegal on members
d:\调试文件夹\test1\test1dlg.h(15) : error C2720: 'l' : 'extern ' storage-class specifier illegal on members
d:\调试文件夹\test1\test1dlg.h(16) : error C2720: 'dc' : 'extern ' storage-class specifier illegal on members
Test1Dlg.cpp
d:\调试文件夹\test1\test1dlg.h(12) : error C2720: 'hbrush' : 'extern ' storage-class specifier illegal on members
d:\调试文件夹\test1\test1dlg.h(13) : error C2720: 'l1' : 'extern ' storage-class specifier illegal on members
d:\调试文件夹\test1\test1dlg.h(14) : error C2720: 're' : 'extern ' storage-class specifier illegal on members
d:\调试文件夹\test1\test1dlg.h(15) : error C2720: 'l' : 'extern ' storage-class specifier illegal on members
d:\调试文件夹\test1\test1dlg.h(16) : error C2720: 'dc' : 'extern ' storage-class specifier illegal on members
请高手再指点下哈
// Test.h
class Test
{
public:
static HBRUSH hbrush;
};// Test.cpp
#include "Test.h"
HBRUSH Test::hbrush = NULL;// Other.cpp
#include "Test.h"
void TestFunction()
{
HBRUSH brush = Test::hbrush;
}
extern static HBRUSH CBAR::hBrush
给你写个简单的例子:
// Test.h
class Test
{
public:
static HBRUSH hbrush;
}; // Test.cpp
#include "Test.h"
HBRUSH Test::hbrush = NULL; // Other.cpp
#include "Test.h"
void TestFunction()
{
HBRUSH brush = Test::hbrush;
}//我觉得cnzdgs 兄写的很清楚啊关于这个问题
//主要是想在类CTEST1DLG中引用这些变量改变后的值
可以在6楼的基础上,定义个Test类的对象
void TestFunction()
{
Test test;
test.Function(); //执行变量值改变的函数
HBRUSH brush = test.hBrush;
}