Why are classes with static data members getting linker errors?
2013-07-17 14:32
671 查看
http://www.parashift.com/c++-faq-lite/link-errs-static-data-mems.html
Because static data
members must be explicitly defined in exactly one compilation unit. If you didn't do this, you'll probably get an "undefined external" linker error. For example:
The linker will holler at you ("Fred::j_ is not defined") unless you define (as opposed to merely declare) Fred::j_ in (exactly) one of your source files:
The usual place to define static data members of class Fred is file Fred.cpp (or Fred.C or
whatever source file extension you use).
Note: in
some cases, you can add = initializer; to the declaration of class-scope static declarations, however if you ever use the data member,
you still need to explicitly define it in exactly one compilation unit. In this case you don't include an = initializer in the definition. A
separate FAQ covers this topic.
Because static data
members must be explicitly defined in exactly one compilation unit. If you didn't do this, you'll probably get an "undefined external" linker error. For example:
// Fred.h class Fred { public: ... private: static int j_; // Declares static data member Fred::j_ ... };
The linker will holler at you ("Fred::j_ is not defined") unless you define (as opposed to merely declare) Fred::j_ in (exactly) one of your source files:
// Fred.cpp #include "Fred.h" int Fred::j_ = some_expression_evaluating_to_an_int; // Alternatively, if you wish to use the implicit 0 value for static ints: // int Fred::j_;
The usual place to define static data members of class Fred is file Fred.cpp (or Fred.C or
whatever source file extension you use).
Note: in
some cases, you can add = initializer; to the declaration of class-scope static declarations, however if you ever use the data member,
you still need to explicitly define it in exactly one compilation unit. In this case you don't include an = initializer in the definition. A
separate FAQ covers this topic.
相关文章推荐
- C++类的静态成员(static members in classes)
- VS2005 error C2864 only static const integral data members can be initialized within a class
- static, const, reference and class object, as datamembers
- DataPump Import Of Object Types Fails With Errors ORA-39083 ORA-2304 Or ORA-39117 ORA-39779 (Doc ID
- only static const integral data members can be initialized within a class
- Avoiding duplicate symbol errors during linking by removing classes from static libraries
- Effective C# Item13: Initialize Static Class Members with Static Constructiors
- Beginning Excel What-If Data Analysis Tools: Getting Started with Goal Seek, Data Tables, Scenarios,
- http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
- Obsolete proguard file; use -keepclasseswithmembers
- static data members
- Static Classes and Static Class Members
- 解决 CoreData 报错 linker command failed with exit code 1 (use -v to see invocation)
- Effective C#之13: Initialize Static Class Members with Static Constructors
- [Windows Azure] Getting Started with Windows Azure SQL Data Sync
- 2 Getting Started with Data Guard
- Getting Started with Core Data
- These are useful functions when you are dealing with URL encoded URLs or POST data.
- Avoiding duplicate symbol errors during linking by removing classes from static libraries
- Static Classes and Static Class Members