c++ - unresolved external symbol "cSocket::cSocket(void)" in function _main -


#ifndef _clientsocket_h_ #define _clientsocket_h_  #include "includes.h" #include "logmsgs.h"  class csocket { public:     csocket();     bool open();     bool listen(char *onip,int onport);     void send(char *msg, int len);     void recv(char *msg,int len);     void close(); protected:     socket  csock;     const int   sndbuf; };  #endif   #include "clientsocket.h"   bool csocket::open() {     wsadata wsadata;     int err;     if((err =wsastartup(0x202, &wsadata)) !=0)     {         error("init wsastartup() failed[%d].", err);         return false;     }     return true; }  bool csocket::listen(char *onip,int onport) {     if(open())     {         //create main socket         csock=socket(af_inet, sock_stream, 0);         if(csock==invalid_socket)         {             int err = wsagetlasterror();             //wsacleanup();             printf("init socket() failed[%d].", err);             return false;         }          //set reuseaddr socket         int     optval = 1;         if(setsockopt(csock, sol_socket, so_reuseaddr, (char *) &optval, sizeof(optval)))         {             int err = wsagetlasterror();             close();             printf("init setsockopt() so_reuseaddr failed[%d].", err);             return false;         }          //set keepalive socket         optval = 1;         if(setsockopt(csock, sol_socket, so_keepalive, (char *) &optval, sizeof(optval)))         {             int err = wsagetlasterror();             close();             printf("init setsockopt() so_keepalive failed[%d].", err);             return false;         }              // set sndbuf socket         if(sndbuf)      // non-0: pointer sndbug         {             optval = sndbuf;             if(setsockopt(csock, sol_socket, so_sndbuf, (char *) &optval, sizeof(optval)))             {                 int err = wsagetlasterror();                 close();                 printf("init setsockopt() so_sndbuf failed[%d].", err);                 return false;             }              // read sndbuf socket             int ret = sizeof(optval);             if(getsockopt(csock, sol_socket, so_sndbuf, (char *) &optval, &ret) == 0)             {                 logmsg("send buffer size socket [%d] k.", optval/1024);             }         }          sockaddr_in     sin;         memset(&sin, 0, sizeof(sin));         sin.sin_family      = af_inet;         sin.sin_addr.s_addr = inet_addr(onip);         sin.sin_port        = htons(onport);         if(bind(csock, (lpsockaddr) &sin, sizeof(sin)))         {             int err = wsagetlasterror();             close();             printf("init bind() failed[%d].", err);             return false;         }          //set non-blocking mode         unsigned long   = 1;         if(ioctlsocket(csock, fionbio, &i))         {             int err = wsagetlasterror();             close();             printf("init ioctlsocket() failed[%d].", err);             return false;         }          //listening port         if(listen(csock, somaxconn))        // somaxconn: win macro definition         {             int err = wsagetlasterror();             close();             printf("init listen() failed[%d].", err);             return false;         }         return true;     } return false; }   void csocket::close() {     closesocket(csock);     wsacleanup(); }   #include "includes.h" #include "logmsgs.h" #include "auth_proto.h" #include "packets.h" #include "clientsocket.h"   int main(int argc, char* argv[])  {     csocket sock;     while(1)     {         sock.open();         sock.listen("127.0.0.1",4444);     }     return 0; } 

error: unresolved external symbol "public: __thiscall csocket::csocket(void)" (??0csocket@@qae@xz) referenced in function _main

what's wrong?

this linker error, means compiler has checked code contains no syntax errors, linker can't find implementation of function you've tried call somewhere in program. in case, function can't find is

__thiscall csocket::csocket(void)" (??0csocket@@qae@xz)  

this function looks cryptic because of name-mangling, compiler takes in-source name of function , transforms in way allows better type-safe linking. however, can still see @ code of function name

csocket::csocket(void) 

this constructor csocket. if you'll notice, in code have defined constructor, why linker can't find implementation it. modifying code adding implementation should fix this.

more generally, though, if ever see error this, means you've promised compiler function or object exists through function prototype or extern declaration, didn't give object file linker containing definition. main causes of (in order);

  1. you prototyped function forgot implement it. that's happened here, think.
  2. you prototyped function, implemented different signature. example, if prototype function void myfunction(int) implement function either void myfunction(int&) or void myfunction(), linker consider overload rather implementation , give error @ link-time rather compile-time.
  3. you compiled code definition, didn't link in. happen if have multi-file project , forget pass 1 of files input linker.

hope helps!


Comments

Post a Comment

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

java - where to store the user credentials in an enterprise application(EAI)? -

java - netbeans "Please wait - classpath scanning in progress..." -