我正在尝试声明该类中的类型对象,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;
}