通过 C++ class 默认参数中的静态数据成员(class 指针)
static data member(class pointer) in default argument via c++ class
我的AvlTreeclass头文件代码如下:
template <typename T>
class AvlTree
{
public:
template <typename T1>
friend class AvlNode;
AvlNode<T> *root;
AvlTree<T>(AvlNode<T> * first=NULL);
static inline AvlNode<T>* & create();
const static AvlNode<T>* _default;
void free(AvlNode<T>* &first=_default){
if (first==NULL) {delete this->root;}
first=NULL;
}
void Insert(AvlNode<T> *&t, T x);
...
}
template <typename T>
inline AvlNode<T>* & AvlTree<T>::create(){
static AvlNode<T>* _default;
return _default;
}
我的主要功能是
int main() {
using namespace std;
AvlTree<int> tree;
int value;
int tmp;
vector<int> value0{10,9,8,7,6,2,3,4,55,5,66,67};
for (auto i = value0.begin(); i != value0.end() ; ++i) {
tree.Insert(tree.root,*i);
}
tree.free();
...
}
我在 AvlTree 的成员函数中出错 class: free()
E:\Cprojects\C1\C3\main.cpp: In member function 'void AvlTree<T>::free(AvlNode<T>*&) [with T = int]':
E:\Cprojects\C1\C3\main.cpp:65:15: error: invalid conversion from 'const AvlNode<int>*' to 'AvlNode<int>*' [-fpermissive]
tree.free();
^
E:\Cprojects\C1\C3\main.cpp:65:15: error: cannot bind rvalue '(AvlNode<int>*)AvlTree<int>::_default' to 'AvlNode<int>*&'
E:\Cprojects\C1\C3\main.cpp: In function 'int main()':
E:\Cprojects\C1\C3\main.cpp:65:15: error: invalid conversion from 'const AvlNode<int>*' to 'AvlNode<int>*' [-fpermissive]
E:\Cprojects\C1\C3\main.cpp:65:15: error: cannot bind rvalue '(AvlNode<int>*)AvlTree<int>::_default' to 'AvlNode<int>*&'
In file included from E:\Cprojects\C1\C3\main.cpp:6:
E:\Cprojects\C1\C3\AvlTree.h:143:10: note: in passing argument 1 of 'void AvlTree<T>::free(AvlNode<T>*&) [with T = int]'
void free(AvlNode<T>* &first=_default){
^~~~
mingw32-make.exe[3]: *** [CMakeFiles\C3.dir\build.make:63: CMakeFiles/C3.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/C3.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/C3.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: C3] Error 2
如果我替换
const static AvlNode* _default;
到
static AvlNode* _default;
我收到这样的错误:
CMakeFiles\C3.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr._ZN7AvlTreeIiE8_defaultE[.refptr._ZN7AvlTreeIiE8_defaultE]+0x0): undefined reference to `AvlTree<int>::_default'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\C3.dir\build.make:180: C3.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/C3.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/C3.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: C3] Error 2
仅 运行 次成功替换代码
AvlNode* &first=_default
至
AvlNode* &first=create()
为什么?这是在带有默认参数的 class 成员函数中成功 运行 的唯一方法吗?
const static AvlNode<T>* _default;
void free(AvlNode<T>* &first=_default){
这些不兼容。由于 _default 是指向 const 对象的指针,因此不能将其传递给期望引用指向可变对象指针的函数。
第二个错误不相关:您还必须定义静态变量,而不仅仅是声明它们:
template <typename T>
AvlNode<T>* AvlTree<T>::_default = nullptr;
我的AvlTreeclass头文件代码如下:
template <typename T>
class AvlTree
{
public:
template <typename T1>
friend class AvlNode;
AvlNode<T> *root;
AvlTree<T>(AvlNode<T> * first=NULL);
static inline AvlNode<T>* & create();
const static AvlNode<T>* _default;
void free(AvlNode<T>* &first=_default){
if (first==NULL) {delete this->root;}
first=NULL;
}
void Insert(AvlNode<T> *&t, T x);
...
}
template <typename T>
inline AvlNode<T>* & AvlTree<T>::create(){
static AvlNode<T>* _default;
return _default;
}
我的主要功能是
int main() {
using namespace std;
AvlTree<int> tree;
int value;
int tmp;
vector<int> value0{10,9,8,7,6,2,3,4,55,5,66,67};
for (auto i = value0.begin(); i != value0.end() ; ++i) {
tree.Insert(tree.root,*i);
}
tree.free();
...
}
我在 AvlTree 的成员函数中出错 class: free()
E:\Cprojects\C1\C3\main.cpp: In member function 'void AvlTree<T>::free(AvlNode<T>*&) [with T = int]':
E:\Cprojects\C1\C3\main.cpp:65:15: error: invalid conversion from 'const AvlNode<int>*' to 'AvlNode<int>*' [-fpermissive]
tree.free();
^
E:\Cprojects\C1\C3\main.cpp:65:15: error: cannot bind rvalue '(AvlNode<int>*)AvlTree<int>::_default' to 'AvlNode<int>*&'
E:\Cprojects\C1\C3\main.cpp: In function 'int main()':
E:\Cprojects\C1\C3\main.cpp:65:15: error: invalid conversion from 'const AvlNode<int>*' to 'AvlNode<int>*' [-fpermissive]
E:\Cprojects\C1\C3\main.cpp:65:15: error: cannot bind rvalue '(AvlNode<int>*)AvlTree<int>::_default' to 'AvlNode<int>*&'
In file included from E:\Cprojects\C1\C3\main.cpp:6:
E:\Cprojects\C1\C3\AvlTree.h:143:10: note: in passing argument 1 of 'void AvlTree<T>::free(AvlNode<T>*&) [with T = int]'
void free(AvlNode<T>* &first=_default){
^~~~
mingw32-make.exe[3]: *** [CMakeFiles\C3.dir\build.make:63: CMakeFiles/C3.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/C3.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/C3.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: C3] Error 2
如果我替换
const static AvlNode* _default;
到
static AvlNode* _default;
我收到这样的错误:
CMakeFiles\C3.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$.refptr._ZN7AvlTreeIiE8_defaultE[.refptr._ZN7AvlTreeIiE8_defaultE]+0x0): undefined reference to `AvlTree<int>::_default'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\C3.dir\build.make:180: C3.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/C3.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/C3.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: C3] Error 2
仅 运行 次成功替换代码
AvlNode* &first=_default
至
AvlNode* &first=create()
为什么?这是在带有默认参数的 class 成员函数中成功 运行 的唯一方法吗?
const static AvlNode<T>* _default;
void free(AvlNode<T>* &first=_default){
这些不兼容。由于 _default 是指向 const 对象的指针,因此不能将其传递给期望引用指向可变对象指针的函数。
第二个错误不相关:您还必须定义静态变量,而不仅仅是声明它们:
template <typename T>
AvlNode<T>* AvlTree<T>::_default = nullptr;