-2

My goal is to build a C++ class to abstract away several POST methods. I reckon I want to have one instance of a CURL object for the entire class, which all the methods share.

Here's my test setup which works fine

Header file

#pragma once
#include <curl/curl.h>
class RPC
{
public:
    RPC();
};

cpp file

RPC::RPC()
{
    CURL* m_curl;
    m_curl = curl_easy_init();
    // curl_easy_setopt() and curl_easy_perform()
    curl_easy_cleanup(m_curl);
}

This code builds and runs and produces the expected output. The problem is when I try to make m_curl a private member of the RPC class. The RPC class will have multiple methods calling curl_easy_perform(), and I do not want to have to call curl_easy_init() and curl_easy_cleanup every time. I want to call them once in the constructor and destructor, respectively.

This is what I want

Header file

#pragma once
#include <curl/curl.h>
class RPC
{
public:
    RPC();
    ~RPC();
    void someMethod();
private:
    CURL* m_curl;
};

cpp file

RPC::RPC()
{
    m_curl = curl_easy_init();
}
RPC::~RPC()
{
    curl_easy_cleanup(m_curl);
}
void RPC::someMethod()
{
    // curl_easy_setopt() and curl_easy_perform()
}

The problem is with this line in the header file:

CURL* m_curl;

I get the following 3 build errors:

error C2143: syntax error: missing ';' before '*'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'

I have also tried calling curl_easy_init() in an initializer list, like this:

RPC::RPC() : m_curl(curl_easy_init())

But that produces the same build errors.

Any help is appreciated! :)

edit: I did include curl in the header.

4
  • 1
    Have your tried #include <curl/curl.h> in the header file?
    – 273K
    Jun 13, 2021 at 7:04
  • Oops, should have included that in the example code. I do have curl included in the header file: #pragma once #include <curl/curl.h> Jun 13, 2021 at 22:17
  • Are you sure you are compiling the same file you have posted? Be sure the file is not modified in the editor and is written on the disk.
    – 273K
    Jun 13, 2021 at 22:55
  • Yes, I am sure that I am compiling that code. Jun 14, 2021 at 3:51

2 Answers 2

1

in the first case you can have CURL include only in the cpp file but for the second case you have to include CURL in the header file.

1
  • curl is included in the header file, I have edited my question to show that Jun 13, 2021 at 22:21
1

Your header needs to look something like this:

#ifndef RPC_H
#define RPC_H

#include "curl/curl.h"

class RPC
{
public:
    RPC();
    ~RPC();
    void someMethod();
private:
    CURL* m_curl;
};

#endif

In other words:

  • You need a declaration for CURL.
  • You do this by #include "curl/curl.h" in your header.
  • Your header also needs an include guard, if you don't already have one.
4
  • curl is included in the header file, I have edited my question to show that Jun 13, 2021 at 22:21
  • And you're using "#pragma once", too. Cool :) Q: Are you still getting the same compile error(s)? Q: Which line(s)? Per your post, I ASSUME all three errors were from line 10 in the header: CURL* m_curl. But "Assuming" is "Bad". Please CONFIRM the line number, and SHOW the corresponding source code. Q: Is it possible you have some weird "#define" somewhere that might be "overriding" the default definition of "CURL"? The line 10 you showed us SHOULD compile with error :(
    – paulsm4
    Jun 13, 2021 at 23:53
  • You are correct, all 3 errors come from the line: CURL* m_curl; The code is exactly as shown, save for the // curl_easy_setopt() and curl_easy_perform() which has some actual curl options set and what not. I also would have expected that to compile just fine. As of right now I have resorted to making a curlWrapper() method which handles the m_curl = curl_easy_init(); and curl_easy_cleanup(m_curl); parts, but that will execute every time I use curl. Oh well... Jun 14, 2021 at 4:02
  • Q: What do you want to bet that the error is caused by one of the lines PRECEDING "private: CURL* m_curl", in something you're not showing us?
    – paulsm4
    Jun 14, 2021 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.