C++ static变量出现 无法解析的外部符号:public: static 的问题

转自:http://bbs.csdn.net/topics/390183594

在头文件中定以后,应在类的生命之外,重新定义一次。

 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 } 
时间: 2024-07-29 06:23:17

C++ static变量出现 无法解析的外部符号:public: static 的问题的相关文章

使用log4cplus时遇到的链接错误:无法解析的外部符号 &quot;public: static class log4cplus::Logger __cdecl log4cplus::Logger::getInstance(class std::basic_string&lt;wchar_t,struct std::char_traits&lt;wchar_t&gt;,

#include "stdafx.h" #include <log4cplus/logger.h> #include <log4cplus/loggingmacros.h> #include <log4cplus/configurator.h> #include <log4cplus/fileappender.h> #include <log4cplus/win32debugappender.h> #include <l

C++程序链接失败,无法解析的外部命令,无法解析的外部符号 &quot;private: static class * Object::current&quot;

C++程序编译结束后,出现链接失败提示: 严重性    代码    说明    项目    文件    行    类别    禁止显示状态错误    LNK2001    无法解析的外部符号 "private: static class Object* Object::current" ([email protected]@@[email protected])    Object    F:\C++\Object.obj    1 严重性    代码    说明    项目    文

error LNK2001: 无法解析的外部符号 &quot;public: char * __thiscall

error LNK2001: 无法解析的外部符号 "public: char * __thiscall CamPinPadCtrl::KeysConvert(unsigned long,char *)" ([email protected]@@[email protected]) 下午有个函数重复使用的地方特别多,所以我想单独在visual studio中给类增加函数,这样每次用到的时候只需调用函数即可 但是我再.h文件中声明函数 char* KeysConvert(unsigned

vs2010+qt4编译出现error LNK2001: 无法解析的外部符号 &quot;public: virtual struct QMetaObject等错误

1.当vs2010编译qt时会出现以下错误: 1>------ 已启动全部重新生成: 项目: MyDialog, 配置: Debug Win32 ------ 1>生成启动时间为 2015/9/9 14:57:04. 1>InitializeBuildStatus: 1>  正在创建"Debug\MyDialog.unsuccessfulbuild",因为已指定"AlwaysCreate". 1>CustomBuild: 1> 

一个小错误:error LNK2019: 无法解析的外部符号 &quot;public: __thiscall Turtle::~Turtle(void)&quot; (??1Turtle@@QAE@XZ),该符号在函数 _main 中被引用

昨天在撸代码的时候遇到了一个十分蛋疼的错误 : 错误: 1>3.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Turtle::~Turtle(void)" (??1Turtle@@QAE@XZ),该符号在函数 _main 中被引用1>3.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Turtle::Turtle(void)" (??0Turtl

C++ 无法解析的外部符号

错误 3 error LNK2001: 无法解析的外部符号 "public: static struct table * security::head" ([email protected]@@[email protected]@A) F:\github\membership\security\security\security.obj 出现这种错误,一般是在类当中声明了静态变量,但是没有在类外进行定义,从而导致此错误,解决错误只需在类外进行定义即可. security.obj : e

无法解析的外部符号 _AtlTraceVU,无法解析的外部符号 ATL::CTrace ATL::CTrace::s_trace

无法解析的外部符号 _AtlTraceVU,该符号在函数 "public: void __cdecl ATL::CTrace::TraceV(char const *,int,unsigned long,unsigned int,wchar_t const *,char *)const " ([email protected]@[email protected]@[email protected]) 中被引用 无法解析的外部符号 "public: static class A

boost 引用错误 无法解析的外部符号

严重性 代码 说明 项目 文件 行 禁止显示状态 错误 LNK2001 无法解析的外部符号 "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" ([email protected]@[email protected]@[email protected]@XZ) libthrift E:\thriftserver\thriftserver\TWins

Qt creator 编译错误:无法解析的外部符号(命令)

问题来自于:只是在creator 中添加了一个新的DIalog类,并在main(),中实例化并show,就出现如下的错误: main.obj:-1: error: LNK2019: 无法解析的外部符号 "public: __cdecl Dialog::Dialog(class QWidget *)" ([email protected]@[email protected]@@@Z),该符号在函数 main 中被引用 main.obj:-1: error: LNK2019: 无法解析的外