Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. C++ 11 std::unique_ptr related compiling error

C++ 11 std::unique_ptr related compiling error

Scheduled Pinned Locked Moved ATL / WTL / STL
helpc++visual-studioperformancetutorial
4 Posts 4 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Falconapollo
    wrote on last edited by
    #1

    I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?

    Quote:

    e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=int 1> ] 1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=int 1> ] 1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)' 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.28 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    here is my code:

    #include
    #include
    #include
    #include

    struct Baa {
    std::unique_ptr x;

    Baa() {
    }
    
    Baa(Baa&& o)
    {
    	x = std::move(o.x);
    }
    Baa operator = (Baa&& param)
    {
    	x = std::move(param.x);
    	return \*this;
    }
    

    };

    int main()
    {
    std::map sheep;
    Baa ba;
    sheep[0] = ba;
    }

    Y T V 3 Replies Last reply
    0
    • F Falconapollo

      I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?

      Quote:

      e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=int 1> ] 1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=int 1> ] 1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)' 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.28 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

      here is my code:

      #include
      #include
      #include
      #include

      struct Baa {
      std::unique_ptr x;

      Baa() {
      }
      
      Baa(Baa&& o)
      {
      	x = std::move(o.x);
      }
      Baa operator = (Baa&& param)
      {
      	x = std::move(param.x);
      	return \*this;
      }
      

      };

      int main()
      {
      std::map sheep;
      Baa ba;
      sheep[0] = ba;
      }

      Y Offline
      Y Offline
      yufengzjj
      wrote on last edited by
      #2

      which compiler you use? with what options?

      1 Reply Last reply
      0
      • F Falconapollo

        I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?

        Quote:

        e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=int 1> ] 1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=int 1> ] 1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)' 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.28 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

        here is my code:

        #include
        #include
        #include
        #include

        struct Baa {
        std::unique_ptr x;

        Baa() {
        }
        
        Baa(Baa&& o)
        {
        	x = std::move(o.x);
        }
        Baa operator = (Baa&& param)
        {
        	x = std::move(param.x);
        	return \*this;
        }
        

        };

        int main()
        {
        std::map sheep;
        Baa ba;
        sheep[0] = ba;
        }

        T Offline
        T Offline
        Theo Buys
        wrote on last edited by
        #3

        Your don't initialize x

        1 Reply Last reply
        0
        • F Falconapollo

          I'm stuck here on how to compile the following code, i need to put a struct which contains std::unique_ptr into std::map, but i always get the error message, anybody can help me?

          Quote:

          e:\console.cpp(21): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> _Ty=int 1> ] 1> d:\programfiles\vs 2010\vc\include\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr' 1> with 1> [ 1> _Ty=int 1> ] 1> This diagnostic occurred in the compiler generated function 'Baa::Baa(const Baa &)' 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.28 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

          here is my code:

          #include
          #include
          #include
          #include

          struct Baa {
          std::unique_ptr x;

          Baa() {
          }
          
          Baa(Baa&& o)
          {
          	x = std::move(o.x);
          }
          Baa operator = (Baa&& param)
          {
          	x = std::move(param.x);
          	return \*this;
          }
          

          };

          int main()
          {
          std::map sheep;
          Baa ba;
          sheep[0] = ba;
          }

          V Offline
          V Offline
          vickoza
          wrote on last edited by
          #4

          the problem is simple. The code

          sheep[0] = ba;

          should be

          sheep[0] = std::move(ba);

          Your first approach used the copy constructor to copy the unique_ptr this defeats the purpose unique_ptr. Also another issue is:

          Baa operator = (Baa&& param)

          should be

          Baa& operator = (Baa&& param)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups