Note that there are some explanatory texts on larger screens.

plurals
  1. POvc++ - static member is showing error
    primarykey
    data
    text
    <p>I am using vc++(2010). I am trying to create a class for server side socket. Here is the header file</p> <pre><code>#include&lt;winsock.h&gt; #include&lt;string&gt; #include&lt;iostream&gt; using namespace std; class AcceptSocket { // static SOCKET s; protected: SOCKET acceptSocket; public: AcceptSocket(){}; void setSocket(SOCKET socket); static void EstablishConnection(int portNo,string&amp;); static void closeConnection(); static void StartAccepting(); virtual void threadDeal(); static DWORD WINAPI MyThreadFunction(LPVOID lpParam); }; SOCKET AcceptSocket::s; </code></pre> <p>and the corresponding source file</p> <pre><code>#include&lt;NetWorking.h&gt; #include&lt;string&gt; void AcceptSocket::setSocket(SOCKET s) { acceptSocket=s; } void AcceptSocket::EstablishConnection(int portno,string &amp;failure) { WSAData w; int error = WSAStartup(0x0202,&amp;w); if(error) failure=failure+"\nWSAStartupFailure"; if(w.wVersion != 0x0202) { WSACleanup(); failure=failure+"\nVersion is different"; } SOCKADDR_IN addr; addr.sin_family=AF_INET; addr.sin_port=htons(portno); addr.sin_addr.s_addr=htonl(INADDR_ANY); AcceptSocket::s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(AcceptSocket::s == INVALID_SOCKET) failure=failure+"\nsocket creating error"; if(bind(AcceptSocket::s,(LPSOCKADDR) &amp;addr,sizeof(addr)) == SOCKET_ERROR) failure=failure+"\nbinding error"; listen(AcceptSocket::s,SOMAXCONN); } void AcceptSocket::closeConnection() { if(AcceptSocket::s) closesocket(AcceptSocket::s); WSACleanup(); } void AcceptSocket::StartAccepting() { sockaddr_in addrNew; int size=sizeof(addrNew); while(1) { SOCKET temp=accept(AcceptSocket::s,(sockaddr *)&amp;addrNew,&amp;size); AcceptSocket * tempAcceptSocket=new AcceptSocket(); tempAcceptSocket-&gt;setSocket(temp); DWORD threadId; HANDLE thread=CreateThread(NULL,0,MyThreadFunction,(LPVOID)tempAcceptSocket,0,&amp;threadId); } } DWORD WINAPI AcceptSocket::MyThreadFunction(LPVOID lpParam) { AcceptSocket * acceptsocket=(AcceptSocket *) lpParam; acceptsocket-&gt;threadDeal(); return 1; } void AcceptSocket::threadDeal() { "You didn't define threadDeal in the derived class"; } </code></pre> <p>Now the main.cpp is</p> <pre><code>#include&lt;Networking.h&gt; int main() { } </code></pre> <p>When I am compiling the error I got is</p> <pre><code>Error 1 error LNK2005: "private: static unsigned int AcceptSocket::s" (?s@AcceptSocket@@0IA) already defined in NetWorking.obj C:\Documents and Settings\prabhakaran\Desktop\check\check\main.obj check Error 2 error LNK1169: one or more multiply defined symbols found C:\Documents and Settings\prabhakaran\Desktop\check\Debug\check.exe 1 1 check </code></pre> <p>What might cause this and how do I solve it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload