无法解析的外部符号“cSocket::cSocket(void)”在函数_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;
}
错误:函数 _main 中引用了无法解析的外部符号“public: __thiscall cSocket::cSocket(void)”(??0cSocket@@QAE@XZ)
什么问题?
#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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个链接器错误,这意味着编译器已检查代码是否不包含语法错误,但链接器无法找到您尝试在程序中某处调用的函数的实现。在这种情况下,它找不到的函数是
这个函数看起来很神秘,因为名称修改,编译器获取函数的源代码名称,然后以允许的方式对其进行转换为了更好的类型安全链接。不过,您仍然可以看到,this 的代码中是函数名称
This is constructor for
cSocket
。如果您会注意到,代码中没有定义此构造函数,这就是链接器找不到它的实现的原因。通过添加实现来修改代码应该有助于解决此问题。不过,更一般地说,如果您看到这样的错误,通常意味着您已经向编译器承诺某个函数或对象通过函数原型或 extern 声明存在,但没有给出包含其定义的链接器的目标文件。造成这种情况的主要原因通常是(大致按此顺序);
void MyFunction(int)
进行原型设计,但随后将该函数实现为void MyFunction(int&)
或void MyFunction()< /code>,链接器会认为这是一个重载而不是实现,并且会在链接时而不是编译时给出错误。
希望这有帮助!
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
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
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);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.Hope this helps!
如果您不打算有构造函数,则您没有声明构造函数定义
,只需从类声明中删除行
cSocket();
You didn't declare a constructor definition
if you didn't intend to have a constructor then just remove the line
cSocket();
from your class declaration