首页 > 解决方案 > C++ 表达式在我的类上必须具有类类型

问题描述

newHTTP对第 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;
    }

我只是不明白我缺少什么,我在 myresponse 上指定了 HINTERNET 并确保 httpConnect 方法返回一个值。有人可以帮忙吗?我的课程代码(当然修剪):

    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;

            }
        }
};

标签: c++class

解决方案


如果您调用一个,请添加默认构造函数。我只看到参数化的一个。


推荐阅读