Cannot declare member function ...to have static linkage"问题
2014-11-04 15:12
549 查看
在GCC下,这是一个warning,然后查找了下原因,是因为static外置导致的。
查看C++ primer
“静态成员函数的声明除了在类体中的函数声明前加上关键字static 以及不能声明为
const 或volatile 之外与非静态成员函数相同出现在类体外的函数定义不能指定关键字
static”
那究竟是为什么呢? 这有用什么目的和用途?
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.
翻译一下:
1. static 在声明处和.cc文件中表示的东西是不同的。
2. static有三种不同的意义:a. 在.cc文件中,static关键字表示函数对外部文件时不可见的,也就是你不应该在.cc文件中使用static来定义一个类的方法或者变量;b. C++中,不允许定义带相同名字的static 实例变量或者函数,因此,如果你声明了一个变量或者函数为静态,那么就不需要再定义处添加static。
然后在CSDN上找一个比较不错的回答:
查看C++ primer
“静态成员函数的声明除了在类体中的函数声明前加上关键字static 以及不能声明为
const 或volatile 之外与非静态成员函数相同出现在类体外的函数定义不能指定关键字
static”
那究竟是为什么呢? 这有用什么目的和用途?
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.
翻译一下:
1. static 在声明处和.cc文件中表示的东西是不同的。
2. static有三种不同的意义:a. 在.cc文件中,static关键字表示函数对外部文件时不可见的,也就是你不应该在.cc文件中使用static来定义一个类的方法或者变量;b. C++中,不允许定义带相同名字的static 实例变量或者函数,因此,如果你声明了一个变量或者函数为静态,那么就不需要再定义处添加static。
然后在CSDN上找一个比较不错的回答:
怎么说呢, 这是一个作用域的问题! 成员函数的作用域是类域, 而在类体外加上static不是表示静态函数,表示的是函数拥有文件域(file scope) 而类域是小于文件域,强行把类域扩大到文件域,就会出错。 如下代码: class CA { public: static void display(void); }; static void CA::display(void) { // ERROR! cout << "Hello CA!" << endl; } int main(int argc, char* argv[]) { CA::display(); }
相关文章推荐
- 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"错误
- 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"错误
- "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
- 提示"illegal reference to data member'CPMAgentManageDlg::m_matrixMatrixSt'in a static member function"
- C++ error:static member function cannot have cv-qualifier
- magento 第四章 出现 "Fatal error:Call to a member function load() on a non-object in \Abstract.php" 问题
- php中出现“Call to a member function Execute() on a non-object”的问题
- 类中静态变量未定义导致undefined reference to static class member问题的解决方法
- Item 45: Use member function templates to accept "all compatible types."(Effective C++)
- 实用程序软件包(关于使用静态函数报错问题“Static function declared but not defined in C+++ "eh.h is only for C++!"报错问题)
- 出现错误"illegal call of non-static member function"?
- error C2894: templates cannot be declared to have 'C' linkage
- non-member function cannot have cv-qualifier
- Win32的项目转成WinCE编译时出现"DllMain function cannot be overloaded"的问题