I have such code with 2 structures:
#include <list>
using namespace std;
struct Connection;
struct User;
typedef list<Connection> Connections;
typedef list<User> Users;
struct User {
Connections::iterator connection;
};
struct Connection {
Users::iterator user;
};
But when I try to compile it, the compiler (C++ Builder XE) return me such error – “Undefined structure ‘Connection’“.
Can anyone help me with my problem?
@ereOn,
struct Connection;
struct User;
struct Connection {
Users::iterator user;
};
typedef list Connections;
typedef list Users;
struct User {
Connections::iterator connection;
};
Undefined structure ‘User’
You’re using incomplete type as type argument to
std::listwhich invokes undefined bevahior according to the C++ Standard.§17.4.3.6/2 says,
So one solution would be using pointer of incomplete type.
This will work because pointer to an incomplete type is a complete type, the compiler can know the size of Connection* which is equal to
sizeof(Connection*)even ifConnectionhasn’t been defined yet.