"Cannot declare member function ...to have static linkage"错误
来源:互联网 发布:网络事件驱动 编辑:程序博客网 时间:2024/04/27 23:21
基本的语法错误,郁闷了我半天,相关的资料可以参考: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 keywordstatic 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 thestatic 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
};
};
- "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++】cannot declare member function *** to have static linkage [-fpermissive]
- error"Cannot declare member function ...to have static linkage"
- Cannot declare member function ...to have static linkage"问题
- static成员"Cannot declare member function ...to have static linkage"错误
- Cannot declare member function ‘static int Foo::bar()’ to have static linkage
- C++ error:static member function cannot have cv-qualifier
- non-member function cannot have cv-qualifier
- error C2894: templates cannot be declared to have 'C' linkage
- error C2894: templates cannot be declared to have 'C' linkage
- error C2894: templates cannot be declared to have 'C' linkage
- reference to non-static member function must be called
- 出现错误"illegal call of non-static member function"?
- How to declare and initialize a static const array as a class member?
- error C2597: illegal reference to data member xxxx in a static member function
- Java多线程编程-线程池的使用
- JAVA--简易聊天室程序
- 79-命名空间
- 9轴陀螺仪KF系列算法的轴向问题
- 80-定义接口
- "Cannot declare member function ...to have static linkage"错误
- Ignatius and the Princess III hdoj 1028 (母函数初学)
- 函数指针回调函数
- 81-继承与实现接口
- hiho 52 连通性一·割边与割点
- 详解ARM的AMBA设备中的DMA设备(Linux驱动之DMA)
- Android安装
- mxnet深度学习(KVS)
- 82-资源resource类型