我正在尝试声明类WRAPPED
中的WRAPPED
类型的对象,而类WRAPPED
包含在另一个名为WRAPPER
的类中。我收到了这些编译器错误。
nested_class_incomplete_type.cpp|56|instantiated from 'WRAPPER<NODE>'|
nested_class_incomplete_type.cpp|62|instantiated from here|
nested_class_incomplete_type.cpp|36|error: 'WRAPPER<T>::WRAPPED::WRAP' has incomplete type|
nested_class_incomplete_type.cpp|33|error: declaration of 'class WRAPPER<NODE>::WRAPPED'|
我也尝试过这样做WRAPPER::WRAPPED WRAP;
但产生了同样的错误。如果WRAPPED
类存在于类之外,这通常不会成为问题,但由于某种原因,它不允许我声明这样一个简单的对象。随意启发C ++编译器语义和神Stroustrup的神奇世界。这是代码。
#include <iostream>
using namespace std;
class NODE
{
int data;
public:
NODE(){}
~NODE(){}
NODE(int data)
{
this->data = data;
}
void print()
{
std::cout<<"data: "<<this->data<<std::endl;
}
};
template <class T>
class WRAPPER
{
public:
static T GLOBAL_WRAPPER_TYPE;
WRAPPER(){}
~WRAPPER(){}
class WRAPPED
{
public:
WRAPPER::WRAPPED WRAP;
WRAPPED(){}
~WRAPPED(){}
void set(T GLOBAL_WRAPPER_TYPE)
{
WRAPPER::GLOBAL_WRAPPER_TYPE = GLOBAL_WRAPPER_TYPE;
}
T& get()
{
return GLOBAL_WRAPPER_TYPE;
}
WRAPPED& operator=(const WRAPPED &INSIDE)
{
GLOBAL_WRAPPER_TYPE = INSIDE.GLOBAL_WRAPPER_TYPE;
return *this;
}
};
WRAPPED INSIDE;
};
template <class T>
T WRAPPER<T>::GLOBAL_WRAPPER_TYPE;
int main()
{
WRAPPER<NODE> WRAPPING;
WRAPPING.INSIDE.set(NODE(99));
NODE temp = WRAPPING.INSIDE.get();
temp.print();
return 0;
}