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. C / C++ / MFC
  4. Problmes using std::map in a class

Problmes using std::map in a class

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminhelpquestion
5 Posts 4 Posters 1 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.
  • R Offline
    R Offline
    Rickard Andersson20
    wrote on last edited by
    #1

    Server.h

    class TcpServer
    {
    ...
    private:
    static map<SOCKET, TcpServer> m_instances;
    ...
    };

    When I build this, the compiler reports this: error C2079: 'std::pair<_Ty1,_Ty2>::second' uses undefined class 'TcpServer' with [ _Ty1=const SOCKET, _Ty2=TcpServer ] Any solution? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

    N J 2 Replies Last reply
    0
    • R Rickard Andersson20

      Server.h

      class TcpServer
      {
      ...
      private:
      static map<SOCKET, TcpServer> m_instances;
      ...
      };

      When I build this, the compiler reports this: error C2079: 'std::pair<_Ty1,_Ty2>::second' uses undefined class 'TcpServer' with [ _Ty1=const SOCKET, _Ty2=TcpServer ] Any solution? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

      N Offline
      N Offline
      Navin
      wrote on last edited by
      #2

      This is a circular definition - you are trying to use TcpServer inside of itself. You might be able to make the map take a pointer and get it to work:private: static map<SOCKET, TcpServer*> m_instances;
      An expert is somebody who learns more and more about less and less, until he knows absolutely everything about nothing.

      1 Reply Last reply
      0
      • R Rickard Andersson20

        Server.h

        class TcpServer
        {
        ...
        private:
        static map<SOCKET, TcpServer> m_instances;
        ...
        };

        When I build this, the compiler reports this: error C2079: 'std::pair<_Ty1,_Ty2>::second' uses undefined class 'TcpServer' with [ _Ty1=const SOCKET, _Ty2=TcpServer ] Any solution? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #3

        You just stumbled onto infinity, believe it or not. :) When the C++ compiler finds a struct or a class it needs to know the size of it, so that it can allocate correct memory buffers to store the objects in. So when it finds a struct, it goes through each member variable, summing up the size of each member. That's the size the struct will get (not considering alignment issues, but that's not important in this case). Simple addition basically. So consider this struct:

        struct X {
        X x;
        }

        What is the size of objects of type X? It's the size of x. So, what's the size of x? Since it's of type X, it's the size of objects of type X. What is the size of objects of type X? ... and so on. Do you see the infinite recursion here? X depends on itself, and thus its size is undefined, and is rejected by the compiler as an undefined type. A type in C++ is not complete (undefined as the compiler said) unless its size is known. Navin suggested you could use pointers instead. How does that help you might wonder? The easy answer is that pointers on most platforms come in one size only (this is not entirely true, as old DOS programmers can testify, but don't worry about that for now). On a 32-bit compiler, pointers are typically 4 bytes large. 32 bits = 4 * 8 bits = 4 * 1 bytes. So, by using pointers instead, the compiler can determine the correct size. -- Arigato gozaimashita!

        R 1 Reply Last reply
        0
        • J Jorgen Sigvardsson

          You just stumbled onto infinity, believe it or not. :) When the C++ compiler finds a struct or a class it needs to know the size of it, so that it can allocate correct memory buffers to store the objects in. So when it finds a struct, it goes through each member variable, summing up the size of each member. That's the size the struct will get (not considering alignment issues, but that's not important in this case). Simple addition basically. So consider this struct:

          struct X {
          X x;
          }

          What is the size of objects of type X? It's the size of x. So, what's the size of x? Since it's of type X, it's the size of objects of type X. What is the size of objects of type X? ... and so on. Do you see the infinite recursion here? X depends on itself, and thus its size is undefined, and is rejected by the compiler as an undefined type. A type in C++ is not complete (undefined as the compiler said) unless its size is known. Navin suggested you could use pointers instead. How does that help you might wonder? The easy answer is that pointers on most platforms come in one size only (this is not entirely true, as old DOS programmers can testify, but don't worry about that for now). On a 32-bit compiler, pointers are typically 4 bytes large. 32 bits = 4 * 8 bits = 4 * 1 bytes. So, by using pointers instead, the compiler can determine the correct size. -- Arigato gozaimashita!

          R Offline
          R Offline
          Rickard Andersson20
          wrote on last edited by
          #4

          I could use a pointer, but what I really don't understand is when a person I met on IRC tried to compile some example code in VC++ 7.1, and it didn't report any errors. I use VC++ 7.0. Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

          M 1 Reply Last reply
          0
          • R Rickard Andersson20

            I could use a pointer, but what I really don't understand is when a person I met on IRC tried to compile some example code in VC++ 7.1, and it didn't report any errors. I use VC++ 7.0. Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #5

            when a compiler compiles != valid code :) For your specific problem of looking up a socket object from an socket handle, I would stick with the pointer solution. You probably don't want to waste memory by storing a copy of the whole object in the container.

            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