Question

I'm trying to declare an object of type WRAPPED that is within the class WRAPPED and the class WRAPPED is contained within another class called WRAPPER. I'm getting these compiler errors.

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'|

I tried doing this too WRAPPER::WRAPPED WRAP; but that yields the same exact error. This normally would not be an issue if the WRAPPED class existed outside a class but for some reason it won't allow me to declare such a simple object. Feel free to enlighten me with the magical world of C++ compiler semantics and the god Stroustrup. Heres the code.

#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;
}
Was it helpful?

Solution

Essentially what you're trying to do is:

class A
{
    A a;
};

int main()
{
    A a;
    return 0;
}

(You can see that this produces the same error here)

This is infinite recursion, you're defining a type using itself. Take a pointer instead, like so:

class A
{
    A* a;
};

int main()
{
    A a;

    return 0;
}

As you can see here this compiles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top