"Cannot declare member function ...to have static linkage"错误
2016-04-28 21:56
579 查看
基本的语法错误,郁闷了我半天,相关的资料可以参考:http://cplusplus.syntaxerrors.info/index.php?title=Cannot_declare_member_function_%E2%80%98static_int_Foo::bar()%E2%80%99_to_have_static_linkage
英文解释:
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 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.
错误的:
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
};
};
英文解释:
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 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.
错误的:
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
};
};
相关文章推荐
- maven 学习---生成基于Maven的项目文档站点
- 远程通信Socket
- 20、Mysql语句快速复习
- Shell编程入门总结(一)
- 79-命名空间
- JAVA--简易聊天室程序
- 记录lua的table转string一个问题
- Java多线程编程-线程池的使用
- 9轴陀螺仪KF系列算法的轴向问题
- 快照(Snapshot)技术发展综述
- 数据库
- maven 学习---将项目安装到Maven本地资源库
- 堆栈的区别
- 最大子数组问题
- UVa 725 Division
- ARM 内核分类
- hiho 51 欧拉路·三
- Android gradle proguard 代码混淆详解
- linux 到windows平台c++工程移植(持续更新)
- 如何卸载Windows 10 内置的App