"Cannot declare member function ...to have static linkage"错误
2013-08-14 15:24
656 查看
转自:http://blog.csdn.net/guoxiaojie_415/article/details/5364354
基本的语法错误,郁闷了我半天,相关的资料可以参考: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
};
};
基本的语法错误,郁闷了我半天,相关的资料可以参考: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
};
};
相关文章推荐
- "Cannot declare member function ...to have static linkage"错误
- "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 ...to have static linkage"错误
- error"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]
- Cannot declare member function ‘static int Foo::bar()’ to have static linkage
- 由"Type mismatch: cannot convert from View to ProgressBar"联想到的类型转换错误的根源
- "List cannot be resolved to a type"错误的解决方法
- C++ error:static member function cannot have cv-qualifier
- "Cannot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- 出现错误"illegal call of non-static member function"?
- 提示"illegal reference to data member'CPMAgentManageDlg::m_matrixMatrixSt'in a static member function"
- magento 第四章 出现 "Fatal error:Call to a member function load() on a non-object in \Abstract.php" 问题
- c++执行程序出现"unable to find a version of the runtime"的错误提示
- "No previous prototype for function" warning警告错误解决
- How to declare and initialize a static const array as a class member?
- 解决 Apache服务器下,访问出现"You don't have permission to …"