Tuesday, 15 January 2013

Namespaces used in multiple C++ source files -



Namespaces used in multiple C++ source files -

i have dll project .h file defines class , related variables in namespace. windows xp using vs 2008. though it's obvious, gstreamer programme :-)

i want variable receiverlist in namespace ipc_receiver, want @ outside namespace in, example, corresponding ipc_sender namespace.

// ipc-receiver..h #ifndef ipcreceiver_h_ #define ipcreceiver_h_ namespace ipc_receiver { .... class declaration. } typedef std::list<ipc_receiver::gstreceiver*> receiverlist_t; typedef std::list<ipc_receiver::gstreceiver*>::iterator receiverlistiter_t; #endif /* ipcreceiver_h_ */

then cpp file variable g_receiverlist declared in ipc_receive namespace not in class.

// ipc-receeiverapis.cpp #include "ipc-receiver.h" namespace ipc_receiver { receiverlist_t g_receiverlist; // list track receivers ... }

and 3rd file

// ipc-receiver.cpp #include "ipc-receiver.h" extern receiverlist_t g_receiverlist; namespace ipc_receiver { gstreceiver::gstreceiver() { g_receiverlist.push_back( ); } }

when compile error:

error 1 error lnk2001: unresolved external symbol "class std::list<class ipc_receiver::gstreceiver *, class std::allocator<class ipc_receiver::gstreceiver *> > g_receiverlist" (?g_receiverlist@@3v?$list@pavgstreceiver@ipc_receiver@ @v?$allocator@pavgstreceiver@ipc_receiver@@@std@@@std@@a) ipc-receiver.obj

i have tried various combinations of adding namespace name in extern , in constructor.

with

ipc_receiver::g_receiverlist.push_back( );

i get:

error 1 error c2039: 'g_receiverlist' : not fellow member of 'ipc_receiver' x:\projects\ipc-dll\ipc-dll\ipc-receiver.cpp 54 ipc-dll

and with

extern receiverlist_t ipc_receiver::g_receiverlist;

i 2 errors:

error 2 error c2039: 'g_receiverlist' : not fellow member of 'ipc_receiver' x:\projects\ipc-dll\ipc-dll\ipc-receiver.cpp 22 error 2 error c2039: 'g_receiverlist' : not fellow member of 'ipc_receiver' x:\projects\ipc-dll\ipc-dll\ipc-receiver.cpp 55

where 22 , 54 lines g_receiverlist used.

using :: instead of ipc_receiver:: not work.

so, how create work: declare variable in namespace in 1 file , utilize in another?

is link order problem?

you need set extern in namespace

namespace ipc_receiver { extern receiverlist_t g_receiverlist; }

and utilize qualified name code not within ipc_receiver namespace.

e.g.

void main() { ipc_receiver::g_receiverlist.push_back(...); }

in illustration in ipc_receiver namespace wouldn't need ipc_receiver:: bit.

c++ namespaces extern

No comments:

Post a Comment