C++ static變量出現 沒法解析的外部符號:public: static 的問題

轉自:http://bbs.csdn.net/topics/390183594ios

在頭文件中定之後,應在類的生命以外,從新定義一次。函數

 

 1 class A  
 2 {
 3 public:
 4 void print()
 5 {
 6 cout<<"a = "<<a<<'\n'<<"b = "<<b<<endl;
 7 cout<<"str = "<<str<<endl;
 8 cout<<"de = "<<de<<endl;
 9 }
10 A():a(0),b(0){}
11 A(int c, double d )
12 {
13   a = c;
14 b = d;
15 }
16 static int sta;
17 private:
18 int a;
19 double b;
20 int de;
21  
22 string str;
23 };
24 <span style="color: #FF0000;">int A::sta = 999;</span>
25 int _tmain(int argc, _TCHAR* argv[])
26 {
27  
28 A a;
29 a.print();
30 cout<< A::sta<<endl;
31 return 0;
32 }

 

// static_test.h : 頭文件  

002 #pragma once  

003    

004 class static_test  

005 {  

006 public:  

007     static_test();//默認構造函數  

008     void set(int x, int y, int z);//成員變量初始化  

009     int add();//  

010     static int add2();//靜態成員函數  

011     ~static_test();  

012     static int sum;//公有的靜態成員變量  

013 private:  

014     static int sum2;//私有的靜態成員變量  

015     int a, b, c;  

016 };  

017    

018    

019 // static_test.cpp : 定義控制檯應用程序的入口點。  

020 //  

021    

022 #include "stdafx.h"  

023 #include "static_test.h"  

024 #include <iostream>  

025 using namespace std;  

026    

027 /*  

028 notice:  

029    若是sum,sum2這兩個靜態成員變量沒有在這裏定義,就會出現錯誤:  

030    static_test.obj : error LNK2001: 沒法解析的外部符號 "private: static int static_test::sum2"   

031    static_test.obj : error LNK2001: 沒法解析的外部符號 "public: static int static_test::sum"   

032 */ 

033 int static_test::sum = 0; //  

034 int static_test::sum2 = 0; //   

035    

036 /*  

037   全局函數能夠調用類的public型的靜態成員變量sum,能夠改變它的值。  

038   但不能用sum2,由於sum2是private類型的。  

039 */ 

040 int fun_add(int x, int y, int z)  

041 {  

042     static_test::sum += x+y+z;  

043     return static_test::sum;  

044 }  

045    

046 /*  

047 成員變量的初始化  

048 */ 

049 static_test::static_test()  

050 {  

051     this->a = 0;  

052     this->b = 0;  

053     this->c = 0;  

054 }  

055    

056 /*  

057 給成員變量賦值  

058 */ 

059 void static_test::set(int x, int y, int z)  

060 {  

061     a = x;  

062     b = y;  

063     c = z;  

064 }  

065    

066 /*  

067 析構函數  

068 */ 

069 static_test::~static_test(void)  

070 {  

071 }  

072    

073 /*  

074 成員函數的實現  

075 */ 

076 int static_test::add()  

077 {  

078     return a+b+c;  

079 }  

080    

081 /*  

082 靜態成員函數的實現  

083 注意:靜態成員函數只能訪問類的靜態成員變量。  

084 定義時,前面不能加static,不然出現error C2724: 「static_test::add2」: 「static」不該在文件範圍內定義的成員函數上使用錯誤:  

085 */ 

086 int static_test::add2()  

087 {  

088     return sum2;  

089 }  

090    

091 int _tmain(int argc, _TCHAR* argv[])  

092 {  

093     int result = 0;  //保存結果  

094     static_test test;//建立一個對象  

095     test.set(1, 2, 3);  

096     result = test.add();  

097     cout<<result<<endl;//result = 6  

098     result = fun_add(4, 5, 6);  

099     cout<<result<<endl;//result = 15  

100     result = fun_add(1, 2, 3);  

101     cout<<result<<endl;//result = 21    由於sum爲靜態成員變量,該變量的值能夠保存給下一次調用,而不會沖掉,直到程序結束爲止。  

102     return 0;  

103 } 
相關文章
相關標籤/搜索