user2023861 user2023861 - 2 months ago 8
C++ Question

What is the error in my shared_ptr declaration?

I have one class that holds a

shared_ptr
to another class. I am getting a compilation error with the
shared_ptr
declaration that says "no members defined using this type." My code to duplicate this is very short:

#include <iostream>
#include <boost/shared_ptr.hpp>

class MyClassImpl
{
};

class MyClass
{
public:
boost::shared_ptr<MyClassImpl> _sptr;
//error C2208: 'boost::shared_ptr<T>' : no members defined using this type
};


int main()
{
MyClass mc;

return 0;
}


What am I doing wrong here? I'm Visual Studio Professional 2010 with Boost 1.54.

Answer

There's no error in your code: it's (surprise) an error in Microsoft's compiler. Apparently _sptr is some kind of magic identifier. Change the name and it will compile correctly (Live at Rextester).

Here's a minimal example that shows the problem:

struct A {
    int _sptr;
};

int main()
{}

This answer implies that both _sptr and _uptr have this effect in Visual C++, I haven't been able to find an authoritative source.

Microsoft does document an extension that implements modifiers __sptr and __uptr that perform some magic when converting 32-bit pointer types to 64-bit pointer types. I assume that the compiler is treating _sptr and _uptr the same as __sptr and __uptr. I've submitted this on Connect as Bug# 882592.

Microsoft's response:

We agree that it is unfortunate for the compiler to disallow the use of these specific identifiers, but it is cost prohibitive to fix this bug in our current implementation due to architectural reasons. In the meantime, please work around this issue by using other names for your variables.