1

I am really confused at the error 'expression must have class type' on newHTTP on line 13

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <WinHttp.h>
    #include "myHTTP.h"

    int main()
    {

        WinHTTP newHTTP();

// error is here
        HINTERNET myResponse = newHTTP.httpConnect(L"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
            L"http://api",
            0,
            L"GET");


        //
        int x;
        std::cin >> x;

        return 0;
    }

I just dont understand what im missing, i have specified HINTERNET on myresponse and made sure the method httpConnect returns a value. Can someone assist? My class code (trimmed of course):

    class WinHTTP {

    private:
        std::string siteUsername, sitePassword;
        std::wstring UA, URL;
        bool bResult = false;
        DWORD dwSize = sizeof(DWORD); // used to handle reading data in bytes
        LPSTR pszOutBuffer; // used to Allocate space for the buffer.
        DWORD dwDownloaded = 0; // set to null if using asynchronously and use in callback function only
        HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;

    public:
        WinHTTP(std::string myuser, std::string mypass) : siteUsername(myuser), sitePassword(mypass){

        }

        // TODO: update to be able to add proxy details either here or before. do check if proxy has been detected in here and open/connect accordingly 
        HINTERNET httpConnect(std::wstring userAgent, std::wstring myURL, int isHTTPS, std::wstring protocol) {

            UA = userAgent;
            URL = myURL;

            std::wstring acceptTypes = L"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";

            int portToUse;
            if (isHTTPS == 1) {
                portToUse = 443;
            }
            else {
                portToUse = 80;
            }

            //initialize http and return session handle -- use c_str to convert wstring to LPCWSTR
            hSession = WinHttpOpen(UA.c_str(),
                WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);

            //make the connection request
            if (hSession) {
                hConnect = WinHttpConnect(hSession, URL.c_str(), portToUse, 0);
            }
            else {
                printf("error: %d",GetLastError());
            }

            // open the request - not connected at this point
            hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);

            if (hRequest) {
                return hRequest;
            }
            else {
                printf("error: %d", GetLastError());
                return hRequest;

            }
        }
};
jimmy
  • 73
  • 1
  • 7
  • 2
    Please edit your question to add a comment on the line you get the errors. And please copy (as text) the full and complete error, including possible informational notes, and paste it into the question without modifications. – Some programmer dude May 25 '18 at 11:26
  • As far as your example shows, you don't have a default constructor for `WinHTTP`, so you should provide two `std::string`s when creating it here: `WinHTTP newHTTP();` – Yksisarvinen May 25 '18 at 11:30
  • 3
    `WinHTTP newHTTP();` -- This does not create an object. This declares a function called `newHTTP()` that takes no arguments and returns a `WinHTTP`. – PaulMcKenzie May 25 '18 at 11:33
  • @Yksisarvinen i do? on line 13 of my class code has constructor – jimmy May 25 '18 at 11:35
  • @PaulMcKenzie your saying i cant create objects like that? everywhere i see thats how you declare a instance of a class – jimmy May 25 '18 at 11:36
  • @jimmy -- No, you don't see this everywhere. Actually you don't find that anywhere in creating an object like that in C++ if the constructor has no arguments. – PaulMcKenzie May 25 '18 at 11:37
  • You have a constructor with 2 parameters. So you need to pass those two parameters to create an object. – Yksisarvinen May 25 '18 at 11:38
  • @PaulMcKenzie so please help me and post an answer? are you saying if i pass parameters to WinHTTP newHTTP(); it will suddenly make it a valid object? – jimmy May 25 '18 at 11:38
  • 1
    @jimmy First, your class must be constructed with two arguments. Second, even if your class takes no arguments as a constructor, the proper way would be `WinHTTP newHTTP;`, as proposed by the links given in the previous comments concerning "most vexing parse". – PaulMcKenzie May 25 '18 at 11:39

1 Answers1

-3

Please add default constructor if you invoking one . I see only parametrized one .