0

obj->ip = "127.0.0.1";行有什么问题?? 我在这里遇到访问冲突。

我记得在课堂上 - 你可以使用私人成员,不是吗?

typedef struct Header
{
    SOCKET sock;
    SOCKADDR_IN addr;
    WORD WSAVersion;
    WSAData data;
    char *ip;
    u_short port;
    int result;
} Header;

class A
{
private:
    std::unique_ptr<Header> obj;

public:
    A(void)
    {
        obj(new Header);
        obj->ip = "127.0.0.1"; // here comes the error!
    }

    ~A(void)
    {
        obj.release();
    }
};

int main(void)
{
    std::unique_ptr<A> handler(new A);
4

2 回答 2

4

obj(new Header);在构造函数主体中是无意义的,并且您正在泄漏内存(完全违背了首先使用智能指针的目的) - 尝试以下操作:

class A
{
    std::unique_ptr<Header> obj;

public:
    A() : obj(new Header()) // value-initialize object, not default-initialize
    {
        obj->ip = "127.0.0.1";
    }
};
于 2012-08-01T03:34:35.270 回答
0

谢谢你们的帮助,伙计们!我已经编辑了我的源代码,现在它运行良好且没有违规,它确实连接到远程主机:

#include <iostream>
#include <memory>
#include <WinSock2.h>
#pragma comment(lib, "Ws2_32.lib")
typedef struct Header
{
    SOCKET sock;
    SOCKADDR_IN addr;
    WORD WSAVersion;
    WSAData data;
    char *ip;
    u_short port;
    int result;
} Header;
class Connector
{
private:
    std::unique_ptr<Header> obj;
public:
    Connector(void)
    {
        obj.reset(new Header);
        obj->ip = "193.201.231.44";
        obj->port = 2775;
        obj->WSAVersion = 0x202;
        obj->addr.sin_family = AF_INET;
        obj->addr.sin_addr.s_addr = inet_addr(obj->ip);
        obj->addr.sin_port = htons(obj->port);
        this->MakeConnection(&obj);
    }
    ~Connector(void)
    {
        obj.release();
    }
    int MakeConnection(std::unique_ptr<Header> *pointer)
    {
        (*pointer)->result = WSAStartup((*pointer)->WSAVersion, &(*pointer)->data);
        if((*pointer)->result != NO_ERROR)
        {
            std::cout << "WSA failed to start with the error: " << std::endl << (*pointer)->result << std::endl;
            return 0;
        }
        else
        {
            std::cout << "WSA started successfully" << std::endl;
            (*pointer)->sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            if((*pointer)->sock == SOCKET_ERROR)
            {
                std::cout << "Failed to init new socket, with the error: " << WSAGetLastError() << std::endl;
                return 0;
            }
            else
            {
                (*pointer)->result = connect(obj->sock, (sockaddr*)&(*pointer)->addr, sizeof((*pointer)->addr));
                if((*pointer)->result == SOCKET_ERROR)
                {
                    std::cout << "Failed to connect to the remote host " << (*pointer)->ip << ":" << (*pointer)->port
                        << ", with the error: " << WSAGetLastError() << std::endl;
                    return 0;
                }
                else
                {
                    std::cout << "Connected to the remote host " << (*pointer)->ip << ":" << (*pointer)->port << std::endl;
                    return 1;
                }
            }
        }
    }
};
void init(void)
{
    std::unique_ptr<Connector> obj;
    obj.reset(new Connector);
    obj.release();
}
int main(void)
{
    init();
    return 0;
}
于 2012-08-01T06:36:11.813 回答