我得到了一個基本的程序運行,但當我嘗試創建一個頭文件和類文件時,我沒有成功。我想知道是否有人可以看我的代碼,看看我哪裏出錯了。我正在Linux上使用文本編輯器並使用G ++進行編譯。標題和源類文件不工作
down.h
#ifndef down_h
#define down_h
#include <string>
class down{
//function of web page retreival
private:
void write_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
}
//webpage retrieval
public:
down();
std::string getString(const char *url){
}
};
#endif
down.cpp
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>
#include <sstream>
#include "down.h"
using namespace std;
//function of web page retreival
size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
std::ostringstream *stream = (std::ostringstream*)userdata;
size_t count = size * nmemb;
stream->write(ptr, count);
return count;
}
//webpage retrieval
std::string getString(const char *url){
CURL *curl;
FILE *fp;
std::ostringstream stream;
CURLcode res;
char outfilename[FILENAME_MAX] = "bbb.txt";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return stream.str();
}
的main.cpp
#include "down.h"
#include <iostream>
int main(void) {
const char *url = "www.google.com";
std::cout << getString("www.google.com");
return 0;
}
不要在頭文件中給函數一個空的主體。 – Unimportant
請了解如何縮進代碼。我不在乎你使用的是什麼縮進方案,但是使用一個。 –