Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a simple and reproducible code, which looks like so:

template <typename T>
class Proxy {
private:
    Wrap<T> &self; // If I comment this, it will work
public:
    Proxy() {}
};

template <typename T>
class Wrap {
    T *p;
public:
    Proxy<T> foo() {
        return Proxy<T>();
    }
};

int main()
{
    return 0;
}

The error I get is:

'Wrap' does not name a type

If I comment Wrap<T> &self, then it will work, but this is not what I need. I need Wrap<T> to be a member of Proxy class. How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
249 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could add forward declaration of Wrap before the class definition of Proxy, otherwise the compiler can't know that it's the name of a class template. It's worth noting that reference data member doesn't require the type to be complete type, so forward declaration is enough here.

// forward declaration
template <typename T>
class Wrap;

template <typename T>
class Proxy {
private:
    Wrap<T> &self;
public:
    Proxy() {}
};

BTW the following issue would be that the reference data member self is not initialized.

LIVE of Clang

error: constructor for 'Proxy' must explicitly initialize the reference member 'self'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...