C++ C++;-未解析的外部符号错误
我一直在四处寻找,终于停止了这件事 我基本上在这里得到了这些错误:C++ C++;-未解析的外部符号错误,c++,header-files,unresolved-external,C++,Header Files,Unresolved External,我一直在四处寻找,终于停止了这件事 我基本上在这里得到了这些错误: 1>Client.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl HTTP_POST_REQUEST(char *,class std::basic
1>Client.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl HTTP_POST_REQUEST(char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?HTTP_POST_REQUEST@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PADV12@1@Z) referenced in function "public: virtual void __thiscall Client::Send(void)" (?Send@Client@@UAEXXZ)
1>G:\Development\C++\Client\Debug\Client.exe : fatal error LNK1120: 1 unresolved externals
1>Client.obj:错误LNK2019:未解析的外部符号“class std::basic_string\uu cdecl HTTP_POST_请求(char*,class std::basic_string,class std::basic_string)”(?HTTP_POST_请求@YA?AV?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@PADV12@1@Z) 函数“public:virtual void\u thiscall Client::Send(void)”中引用(?Send@Client@@UAEXXZ)
1> G:\Development\C++\Client\Debug\Client.exe:致命错误LNK1120:1未解析的外部
据我所知,我认为是Client.obj没有使用该程序编译(不要引用我的话)?
但我读到可能是一些链接问题或类似的问题。对我来说,似乎我做的每件事都是对的(但显然我没有)
客户端.cpp
#include <iostream>
#include <string>
using namespace std;
#include "HttpUtils.h"
#include "Client.h"
Client::Client() {
cout << "Works!" << endl;
}
void Client::Send() {
string res = HTTP_POST_REQUEST("localhost", "/test.php", "data1=data&data2=data");
cout << res << endl;
}
#include <iostream>
#include <string>
using namespace std;
#include "Client.h"
int main(int argc, char *argv[]) {
Client mainClient;
Client* client1 = &mainClient;
client1->Send();
cin.get();
}
#include <stdlib.h>
#include <winsock.h>
#include <sstream>
#pragma comment (lib, "wsock32.lib")
#include <iostream>
#include <string>
using namespace std;
#include "HttpUtils.h"
void die_with_error(char *errorMessage) {
cerr << errorMessage << endl;
cin.get();
exit(1);
}
void die_with_wserror(char *errorMessage) {
cerr << errorMessage << ": " << WSAGetLastError() << endl;
cin.get();
exit(1);
}
string HTTP_POST_REQUEST(char *hostname, string path, string data) {
string request;
string response;
int resp_leng;
char buffer[BUFFERSIZE];
struct sockaddr_in serveraddr;
int sock;
WSADATA wsaData;
hostent *remoteHost;
int port = 80;
stringstream ss;
ss << data.length();
stringstream request2;
request2 << "POST " << path << " HTTP/1.1" << endl;
request2 << "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" << endl;
request2 << "Host: " << hostname << endl;
request2 << "Content-Length: " << data.length() << endl;
request2 << "Content-Type: application/x-www-form-urlencoded" << endl;
request2 << "Accept-Language: en-uk" << endl;
request2 << endl;
request2 << data;
request = request2.str();
cout << request << endl << "###################################################" << endl << endl;
// Init WinSock.
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
die_with_wserror("WSAStartup() failed");
// Open socket.
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
die_with_wserror("socket() failed");
// Convert hostname into IP
remoteHost = gethostbyname(hostname);
char *temp;
if (remoteHost != NULL) {
temp = remoteHost->h_addr_list[0];
} else {
temp = NULL;
return "Error: Invalid hostname passed to HTTP_POST().";
}
// Connect.
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
int i = 0;
if (remoteHost->h_addrtype == AF_INET)
{
while (remoteHost->h_addr_list[i] != 0) {
serveraddr.sin_addr.s_addr = *(u_long *)remoteHost->h_addr_list[i++];
}
}
serveraddr.sin_port = htons((unsigned short)port);
if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
die_with_wserror("connect() failed");
// Send request.
if (send(sock, request.c_str(), request.length(), 0) != request.length())
die_with_wserror("send() sent a different number of bytes than expected");
// Get response.
response = "";
resp_leng = BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
resp_leng = recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng>0)
response += string(buffer).substr(0, resp_leng);
//note: download lag is not handled in this code
}
// Disconnect
closesocket(sock);
// Cleanup
WSACleanup();
//response.erase(0, 184);
return response;
}
#包括
#包括
使用名称空间std;
#包括“HttpUtils.h”
#包括“Client.h”
客户机::客户机(){
无法确保将HttpUtils.cpp添加到make文件/命令中。听起来好像HttpUtils.cpp没有编译。这就是客户端找不到HttpUtils符号的原因!确保将HttpUtils.cpp添加到make文件/命令中。听起来好像HttpUtils.cpp没有编译。这就是客户端找不到HttpUtils符号的原因!你真的这么认为吗在你的main
中有Client->Send();
?不应该是client1->Send();
?在复制代码时,这只是一个输入错误,但错误仍然存在。HTTP\u POST\u REQUEST()在哪里
implemented?是否有您忘记链接的库?未解析的外部符号
当它找到您正在调用的函数的原型,但找不到实现时,通常会出现错误,因此请仔细检查项目中是否包含HttpUtils.cpp
。是否存在可能的重复项在你的main
中有aveClient->Send();
?不应该是client1->Send();
?在复制代码时,这只是一个输入错误,但错误仍然存在。HTTP\u POST\u REQUEST()在哪里
implemented?是否有您忘记链接的库?未解析的外部符号
当它找到您正在调用的函数的原型但找不到实现时,通常会出现错误,因此请仔细检查HttpUtils.cpp
是否包含在您的项目中。可能存在重复的
#include <stdlib.h>
#include <winsock.h>
#include <sstream>
#pragma comment (lib, "wsock32.lib")
#include <iostream>
#include <string>
using namespace std;
#include "HttpUtils.h"
void die_with_error(char *errorMessage) {
cerr << errorMessage << endl;
cin.get();
exit(1);
}
void die_with_wserror(char *errorMessage) {
cerr << errorMessage << ": " << WSAGetLastError() << endl;
cin.get();
exit(1);
}
string HTTP_POST_REQUEST(char *hostname, string path, string data) {
string request;
string response;
int resp_leng;
char buffer[BUFFERSIZE];
struct sockaddr_in serveraddr;
int sock;
WSADATA wsaData;
hostent *remoteHost;
int port = 80;
stringstream ss;
ss << data.length();
stringstream request2;
request2 << "POST " << path << " HTTP/1.1" << endl;
request2 << "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" << endl;
request2 << "Host: " << hostname << endl;
request2 << "Content-Length: " << data.length() << endl;
request2 << "Content-Type: application/x-www-form-urlencoded" << endl;
request2 << "Accept-Language: en-uk" << endl;
request2 << endl;
request2 << data;
request = request2.str();
cout << request << endl << "###################################################" << endl << endl;
// Init WinSock.
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
die_with_wserror("WSAStartup() failed");
// Open socket.
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
die_with_wserror("socket() failed");
// Convert hostname into IP
remoteHost = gethostbyname(hostname);
char *temp;
if (remoteHost != NULL) {
temp = remoteHost->h_addr_list[0];
} else {
temp = NULL;
return "Error: Invalid hostname passed to HTTP_POST().";
}
// Connect.
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
int i = 0;
if (remoteHost->h_addrtype == AF_INET)
{
while (remoteHost->h_addr_list[i] != 0) {
serveraddr.sin_addr.s_addr = *(u_long *)remoteHost->h_addr_list[i++];
}
}
serveraddr.sin_port = htons((unsigned short)port);
if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
die_with_wserror("connect() failed");
// Send request.
if (send(sock, request.c_str(), request.length(), 0) != request.length())
die_with_wserror("send() sent a different number of bytes than expected");
// Get response.
response = "";
resp_leng = BUFFERSIZE;
while (resp_leng == BUFFERSIZE)
{
resp_leng = recv(sock, (char*)&buffer, BUFFERSIZE, 0);
if (resp_leng>0)
response += string(buffer).substr(0, resp_leng);
//note: download lag is not handled in this code
}
// Disconnect
closesocket(sock);
// Cleanup
WSACleanup();
//response.erase(0, 184);
return response;
}
#pragma once
#include <iostream>
#include <string>
using namespace std;
#ifndef HTTP_UTILS__H
#define HTTP_UTILS__H
#define BUFFERSIZE 1024
void die_with_error(char *errorMessage);
void die_with_wserror(char *errorMessage);
string HTTP_POST_REQUEST(char *hostname, string path, string data);
#endif