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 the 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 the 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 the 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 the 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 the 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 to non-blocking mode
unsigned long i = 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 is a linker error, which means that the compiler has checked that the code contains no syntax errors, but the linker can't find an implementation of a function you've tried to call somewhere in the program. In this case, the function it can't find is
__thiscall cSocket::cSocket(void)" (??0cSocket@@QAE@XZ)
This function looks cryptic because of name-mangling, where the compiler takes the in-source name of a function and then transforms it in a way that allows for better type-safe linking. However, you can still see that at the code of this is the function name
cSocket::cSocket(void)
This is constructor for cSocket
. If you'll notice, nowhere in the code have you defined this constructor, which is why the linker can't find an implementation for it. Modifying your code by adding an implementation should help fix this.
More generally, though, if you ever see an error like this, it usually means that you've promised the compiler that some function or object exists through a function prototype or extern
declaration, but didn't give an object file to the linker containing its definition. The main causes of this usually are (in roughly this order);
- You prototyped a function but forgot to implement it. That's what happened here, I think.
- You prototyped a function, but then implemented it with a different signature. For example, if you prototype a function
void MyFunction(int)
but then implement the function either asvoid MyFunction(int&)
or asvoid MyFunction()
, the linker will consider this an overload rather than an implementation and will give you the error at link-time rather than compile-time. - You compiled code with the definition, but didn't link it in. This could happen if you have a multi-file project and then forget to pass one of the files as input to the linker.
Hope this helps!
You didn't declare a constructor definition
cSocket::cSocket()
{
//your init code here
}
if you didn't intend to have a constructor then just remove the line cSocket();
from your class declaration
精彩评论