当我在课堂上包装libcurl时,我遇到了问题:
Foo::Foo()
{
curl = curl_easy_init();
if (!curl)
throw std::runtime("Can't initialize libcurl");
}
char* Foo::GetPage(char *url)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, WriteData );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)data);
CURLcode res = curl_easy_perform( curl );
}
我在GetPage函数的第一行(设置URL)遇到了段错误。如果我在GetPage中启动curl-一切正常。还有其他人遇到类似的问题吗?
最佳答案
变量失去作用域。当curl变量进入GetPage函数时,检查其值。