VS2019 C++成员函数LNK2005错误

VS2019 C++ Member Functions LNK2005 Error

输出:

1>------ Rebuild All started: Project: SecureChatting, Configuration: Debug Win32 ------
1>Connection.cpp
1>Functions.cpp
1>SecureChatting.cpp
1>C:\Users\maxip\Desktop\C++\SecureChatting\SecureChatting.cpp(2,10): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>C:\Users\maxip\Desktop\C++\SecureChatting\SecureChatting.cpp(3,10): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>Generating Code...
1>SecureChatting.obj : error LNK2005: "private: void __thiscall Connection::Accepter(void)" (?Accepter@Connection@@AAEXXZ) already defined in Connection.obj
1>SecureChatting.obj : error LNK2005: "private: void __thiscall Connection::Bind(unsigned int)" (?Bind@Connection@@AAEXI@Z) already defined in Connection.obj
1>SecureChatting.obj : error LNK2005: "private: void __thiscall Connection::Close(void)" (?Close@Connection@@AAEXXZ) already defined in Connection.obj
1>SecureChatting.obj : error LNK2005: "private: void __thiscall Connection::CloseListener(unsigned int)" (?CloseListener@Connection@@AAEXI@Z) already defined in Connection.obj
1>SecureChatting.obj : error LNK2005: "private: void __thiscall Connection::CreateSock(void)" (?CreateSock@Connection@@AAEXXZ) already defined in Connection.obj
1>SecureChatting.obj : error LNK2005: "public: void __thiscall Connection::Initialize(void)" (?Initialize@Connection@@QAEXXZ) already defined in Connection.obj
1>SecureChatting.obj : error LNK2005: "private: void __thiscall Connection::Listen(unsigned int)" (?Listen@Connection@@AAEXI@Z) already defined in Connection.obj
1>C:\Users\maxip\Desktop\C++\SecureChatting\Debug\SecureChatting.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Done building project "SecureChatting.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

错误列表:IMAGE

我的代码:

#include <iostream>
#include <WS2tcpip.h>
#pragma comment (lib,"ws2_32.lib")
#pragma once

using namespace std;

class Connection
{
public:
    SOCKET clientSocket;

    // Initialize winsock
    void Initialize();

private:
    //Create Socket
    void CreateSock();

    //Bind the ip address and port to a socket
    void Bind(SOCKET listening);

    //Tell Winsock the socket is for listening & whait for a connection
    void Listen(SOCKET listening);

    // Close listening socket
    void CloseListener(SOCKET listening);

    //While loop: accept and echo message back to client
    void Accepter();

    //Close the socket & cleanup Winsock
    void Close();
};

void Connection::Initialize() {
    
    WSADATA wsData;
    WORD ver = MAKEWORD(2, 2);

    int wsOK = WSAStartup(ver, &wsData);
    if (wsOK != 0)
    {
        cerr << "Can't initialize winsock! Quitting.." << endl;
        return;
    }
    CreateSock();
}

void Connection::CreateSock() {
    SOCKET listening = socket(AF_INET, SOCK_STREAM,0);

    if (listening == INVALID_SOCKET) {
        cerr << "Can't create socket! Quitting.." << endl;
        return;
    }
    Bind(listening);
}

void Connection::Bind(SOCKET listening)
{
    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(54000);
    hint.sin_addr.S_un.S_addr = INADDR_ANY;
     
    bind(listening, (sockaddr*)&hint, sizeof(hint));
    Listen(listening);
}

void Connection::Listen(SOCKET listening)
{
    listen(listening, SOMAXCONN);

    sockaddr_in client;
    int clientsize = sizeof(client);

    clientSocket = accept(listening, (sockaddr*)&client, &clientsize);
    if (clientSocket == INVALID_SOCKET) {
        cerr << "Invalid socket, can't accept! Quitting.." << endl;
        return;
    }

    char host[NI_MAXHOST];
    char service[NI_MAXSERV];

    ZeroMemory(host, NI_MAXHOST);
    ZeroMemory(service, NI_MAXSERV);

    if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXHOST, 0) == 0) 
    {
        cout << host << " connected on port " << service << endl;
    }
    else
    {
        inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
        cout << host << " connected on port " << ntohs(client.sin_port) << endl;
    }

    CloseListener(listening);
}

void Connection::CloseListener(SOCKET listening)
{
    closesocket(listening);
    
    Accepter();
}

void Connection::Accepter()
{
    char buf[4000];

    while(0)
    {
        ZeroMemory(buf, 4096);

        //Wait for client to send data
        int bytesReceived = recv(clientSocket, buf, 4096,0);
        if(bytesReceived == SOCKET_ERROR)
        {
            cerr << "Error in recv(). Quitting.." << endl;
            break;
        }
        if(bytesReceived == 0)
        {
            cout << "Client disconnected.." << endl;
            break;
        }

        //Echo message back to client
        send(clientSocket, buf, bytesReceived + 1, 0);
    }

    Close();
}

void Connection::Close()
{
    closesocket(clientSocket);
    WSACleanup();
}‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎

我不知道我搞砸了什么。只有当我尝试编译或 运行 版】 ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀††† ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀†=⠀=†1

最有可能的情况是您将 header 包含到多个文件中。您可以

  1. 将成员函数定义移动到 .cpp 文件中。
  2. 使所有成员函数内联。