无法解析的外部符号“cSocket::cSocket(void)”在函数_main中

发布于 2024-10-17 13:29:43 字数 3828 浏览 2 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哀由 2024-10-24 13:29:43

这是一个链接器错误,这意味着编译器已检查代码是否不包含语法错误,但链接器无法找到您尝试在程序中某处调用的函数的实现。在这种情况下,它找不到的函数是

__thiscall cSocket::cSocket(void)" (??0cSocket@@QAE@XZ) 

这个函数看起来很神秘,因为名称修改,编译器获取函数的源代码名称,然后以允许的方式对其进行转换为了更好的类型安全链接。不过,您仍然可以看到,this 的代码中是函数名称

cSocket::cSocket(void)

This is constructor for cSocket。如果您会注意到,代码中没有定义此构造函数,这就是链接器找不到它的实现的原因。通过添加实现来修改代码应该有助于解决此问题。

不过,更一般地说,如果您看到这样的错误,通常意味着您已经向编译器承诺某个函数或对象通过函数原型或 extern 声明存在,但没有给出包含其定义的链接器的目标文件。造成这种情况的主要原因通常是(大致按此顺序);

  1. 您设计了一个函数原型,但忘记实现它。我认为,这就是这里发生的情况。
  2. 您设计了一个函数原型,但随后使用不同的签名实现了它。例如,如果您对函数 void MyFunction(int) 进行原型设计,但随后将该函数实现为 void MyFunction(int&)void MyFunction()< /code>,链接器会认为这是一个重载而不是实现,并且会在链接时而不是编译时给出错误。
  3. 您使用定义编译了代码,但没有链接它。如果您有一个多文件项目,然后忘记将其中一个文件作为输入传递给链接器,则可能会发生这种情况。

希望这有帮助!

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);

  1. You prototyped a function but forgot to implement it. That's what happened here, I think.
  2. 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 as void MyFunction(int&) or as void 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.
  3. 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!

离鸿 2024-10-24 13:29:43

如果您不打算有构造函数,则您没有声明构造函数定义

cSocket::cSocket()
{
    //your init code here
}

,只需从类声明中删除行 cSocket();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文