我見過很多這樣的其他問題,但我無法在他們的幫助下找出這個問題。我知道這是一個鏈接問題,但從我所看到的,我已經理清了鏈接。LNK2019:無法解析的外部符號
我在寫一個聊天服務器/客戶端(在this article的幫助下)。
我已經定義了一個類來容納服務器函數,並且有一個頭文件來處理所有的包含。
這是頭文件:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
#include "resource1.h"
class ChatServer
{
public: int InitServer(HINSTANCE hInst);
public: void ReportError(int errorCode, const char *whichFunc);
};
這是實際的服務器「類」:
#include "server.h"
#define NETWORK_ERROR -1
#define NETWORK_OK 0
//Keeps stuff for the server
int ChatServer::InitServer(HINSTANCE hInst)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
sockVersion = MAKEWORD(1,1); //Version 1.1
//Init winsock
WSAStartup(sockVersion, &wsaData);
//Create listening socket
SOCKET listeningSocket;
//AFINET - Go over TCP
//SOCK_STREAM - Stream oriented socket
//IPPROTO_TCP - Use tcp rather than udp
listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listeningSocket == INVALID_SOCKET)
{
nret = WSAGetLastError(); //Get error detail
ReportError(nret, "socket()");
WSACleanup();
return NETWORK_ERROR;
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(1337);
//Bind the socket to local server address.
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
if(nret == SOCKET_ERROR)
{
nret = WSAGetLastError();
ReportError(nret, "bind()");
WSACleanup();
return NETWORK_ERROR;
}
//Make socket listen
nret = listen(listeningSocket, 10); //Up to 10 connections at the same time.
if(nret = SOCKET_ERROR)
{
nret = WSAGetLastError();
ReportError(nret, "listen()");
WSACleanup();
return NETWORK_ERROR;
}
//Wait for client
SOCKET theClient;
theClient = accept(listeningSocket, NULL, NULL);
if(theClient == INVALID_SOCKET)
{
nret = WSAGetLastError();
ReportError(nret, "accept()");
WSACleanup();
return NETWORK_ERROR;
}
//Send and receive from the client, and finally,
closesocket(theClient);
closesocket(listeningSocket);
//shutdown
WSACleanup();
return NETWORK_OK;
}
void ChatServer::ReportError(int errorCode, const char *whichFunc)
{
char errorMsg[92]; // Declare a buffer to hold
// the generated error message
ZeroMemory(errorMsg, 92); // Automatically NULL-terminate the string
// The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
}
最後,與程序調用入門方法的main.cpp文件「的ChatServer :: InitServer(g_hInst)」。這是非常大的,所以我省略了它,但如果需要的話,我會發布它。
我收到錯誤消息就像是一個下方,但他們所有的狀態問題,涉及到winsockets API的API函數:
Error 3 error LNK2019: unresolved external symbol [email protected] referenced in function "public: int __thiscall ChatServer::InitServer(struct HINSTANCE__ *)" ([email protected]@@[email protected]@@Z)
正如我前面所說,我相信這個問題有東西編譯器會誤解如何處理應該鏈接到winsock.h的「closesocket」函數。
感謝所有的任何意見,並感謝閱讀這一切廢話:)的
你爲什麼不使用winsock2.h? – Drahakar
你有鏈接與wininet.lib和ws2_32.lib? – stijn
您不需要在每個成員函數聲明前加上'public'。一旦列出訪問說明符,在列出不同的訪問說明符之前,所有後續內容都將具有相同的訪問權限。 – Praetorian