Problmes using std::map in a class
-
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 -
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: 37318This 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. -
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: 37318You just stumbled onto infinity, believe it or not. :) When the C++ compiler finds a
struct
or aclass
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!
-
You just stumbled onto infinity, believe it or not. :) When the C++ compiler finds a
struct
or aclass
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!
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
-
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