Cannot declare member function ‘static int Foo::bar()’ to have static linkage
2011-07-27 16:50
501 查看
You can get the error
The reason is that static means something different inside .cc files than in class declarations It is really stupid, but the keyword static has three different meanings. In the .cc file, the static keyword means that the function isn't visible to any code outside of that particular file.
This means that you shouldn't use static in a .cc file to define one-per-class methods and variables. Fortunately, you don't need it. In C++, you are not allowed to have static variables or static methods with the same name(s) as instance variables or instance methods. Therefore if you declare a variable or method as static in the class declaration, you don't need the static keyword in the definition. The compiler still knows that the variable/method is part of the class and not the instance.
WRONG
ALSO WORKS
转自 http://cplusplus.syntaxerrors.info/index.php?title=Cannot_declare_member_function_%E2%80%98static_int_Foo::bar%28%29%E2%80%99_to_have_static_linkage
cannot declare member function ‘static int Foo::bar()’ to have static linkageif you declare a method to be static in your .cc file.
The reason is that static means something different inside .cc files than in class declarations It is really stupid, but the keyword static has three different meanings. In the .cc file, the static keyword means that the function isn't visible to any code outside of that particular file.
This means that you shouldn't use static in a .cc file to define one-per-class methods and variables. Fortunately, you don't need it. In C++, you are not allowed to have static variables or static methods with the same name(s) as instance variables or instance methods. Therefore if you declare a variable or method as static in the class declaration, you don't need the static keyword in the definition. The compiler still knows that the variable/method is part of the class and not the instance.
WRONG
Foo.h: class Foo { public: static int bar(); }; Foo.cc: static int Foo::bar() { // stuff }WORKS
Foo.h: class Foo { public: static int bar(); }; Foo.cc: int Foo::bar() { // stuff }A way to bypass this problem is to embed the defintion in the .h file, but this causes the function to be inline by default.
ALSO WORKS
Foo.h: class Foo { public: static int bar() { // stuff }; };
转自 http://cplusplus.syntaxerrors.info/index.php?title=Cannot_declare_member_function_%E2%80%98static_int_Foo::bar%28%29%E2%80%99_to_have_static_linkage
相关文章推荐
- 【c++】cannot declare member function *** to have static linkage [-fpermissive]
- static成员"Cannot declare member function ...to have static linkage"错误
- "Cannot declare member function ...to have static linkage"错误
- "Cannot declare member function ...to have static linkage"错误
- error"Cannot declare member function ...to have static linkage"
- "Cannot declare member function ...to have static linkage"错误
- "Cannot declare member function ...to have static linkage"错误
- "Cannot declare member function ...to have static linkage"错误
- Cannot declare member function ...to have static linkage"问题
- C++ error:static member function cannot have cv-qualifier
- reference to non-static member function must be called
- invalid use of non-static member function ‘void Date::init(int, int, int)’
- How to declare and initialize a static const array as a class member?
- How do I declare and use a pointer to a class member function?
- error C2440: ‘static_cast’ : cannot convert from ‘UINT (__thiscall CMenuBar::* )(CPoint)’ to ‘LRESUL
- non-member function cannot have cv-qualifier
- error C2894: templates cannot be declared to have 'C' linkage
- 'static' can indeed be used in C++ to create a Static Member Function
- error C2894: templates cannot be declared to have 'C' linkage
- cannot add a child that doesnot have a YogoNode to a parent without a measure function!