首页 > 其他 > 详细

"Cannot declare member function ...to have static linkage"错误

时间:2015-11-20 19:04:49      阅读:350      评论:0      收藏:0      [点我收藏+]
英文解释:

if 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 isnt visible to any code outside of that particular file.

This means that you shouldnt use static in a .cc file to define one-per-class methods and variables. Fortunately, you dont 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 dont need the static keyword in the definition. The compiler still knows that the variable/method is part of the class and not the instance.

翻译: 

如果在你的.cc文件,你声明的方法是静态的。静态意味着的.cc文件不同于类的声明。
但关键字static有三个不同的含义。
在.cc文件,静态关键字意味着该功能是任何代码该文件外部不可见的。这意味着,你不应该使用静态的.cc文件来定义一个每类方法和变量。
在C ++中,你不能有静态变量或静态方法在实例声明和实例方法中具有相同的名称(S)。
因此,如果你在类声明声明一个变量或方法为静态的,你不需要在定义static关键字在.cc文件。
编译器仍然知道该变量/方法是类的一部分,而不是该实例。


错误的:
 Foo.h:
class Foo 
{
   public: 
     static int bar();
};
Foo.cc:
static int Foo::bar() 
{
   // stuff
}
正确的:
 Foo.h:
class Foo 
{
   public: 
     static int bar();
};
Foo.cc:
int Foo::bar() 
{
   // stuff
}
这个也是正确的:
 Foo.h:
class Foo 
{
   public: 
     static int bar()
       {
         // stuff
       };
};

 

"Cannot declare member function ...to have static linkage"错误

原文:http://www.cnblogs.com/zendu/p/4981471.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!