【发布时间】:2020-12-23 15:34:33
【问题描述】:
我正在尝试使用wininet 的InternetCheckConnection 函数检查互联网连接是否存在。
这是我的 CheckerClass:,它处理检查过程。
#pragma once
#include <Windows.h>
#include <wininet.h>
#pragma comment(lib,"wininet.lib")
#include <String>
public ref class CheckerClass
{
public:
static std::string hasInternet() {
bool bConnect = InternetCheckConnection(L"https://www.google.com", FLAG_ICC_FORCE_CONNECTION, 0);
if (bConnect){
return "Has Internet!";
}else{
return "No Internet!";
}
}
};
但我收到以下错误,我无法解决。
Error (active) E1986 an ordinary pointer to a C++/CLI ref class or interface class is not allowed
Error (active) E0266 "IServiceProvider" is ambiguous
Error C3699 '*': cannot use this indirection on type 'IServiceProvider'
经过搜索,我发现这是因为使用using namespace System,但我在上面的类中没有。
但是,我在使用上述类的 Main 类中有以下内容。
#pragma once
#include<string>
#include "CheckerClass.h"
namespace CppCLRWinformsProjekt {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using std::string;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
CheckerClass checkerClass;
Form1(void)
{
InitializeComponent();
string result = checkerClass.hasInternet();
this->label_output->Text = gcnew System::String(result.c_str());
}
.....
谁能解释一下发生了什么,我该如何解决上述问题?
【问题讨论】:
-
这些是 .h 文件,您还没有找到#includes 的 .cpp 文件。并包含使用命名空间系统,从而搞砸了 Windows.h 声明。您应该得到诊断的一个明显错误是 checkerClass 变量,必须声明为 CheckerClass^。
-
@HansPassant - 我认为课程有问题。
标签: c++ visual-studio visual-studio-2019