繁体   English   中英

标准::向量<std::string>和 push_back

[英]std::vector<std::string> and push_back

我经营着一堆网站,所以我试图使用

#include <stdio.h>
#include <tchar.h>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <windows.h>
//.......................................
#include "stdafx.h"

 HWND vbrowser;
 std::vector<std::string> sites;
 sites.push_back("https://hardcoregames.azurewebsites.net/wp-admin/");


 wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
 {
//  wchar_t* wString = new wchar_t[4096];
    MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
    return wString;
 }

 int main()
{
     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    ShellExecute(vbrowser, convertCharArrayToLPCWSTR("open"), convertCharArrayToLPCWSTR(url.c_str()), NULL, NULL, NULL);
     return 0;
 }

所以我在想,我可以对网站进行硬编码,我只操作几个

但是当我尝试使用时:

 sites.push_back ("https://hardcoregames.azurewebsites.net/wp-admin/");

vc 2017 不喜欢那个,说没有存储类或类型说明符,然后它建议我使用分号;

我的项目是打开管理页面,然后等待一段时间再进入下一个站点

vc 2017 社区版

我希望能够在 wordpress 中调出管理员屏幕

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
ShellExecute(vbrowser, convertCharArrayToLPCWSTR("open"), convertCharArrayToLPCWSTR(url.c_str()), NULL, NULL, NULL);

您正在函数外部调用成员函数 - 这在 C++ 中是非法的,除非初始化变量。 具体来说,这一行:

sites.push_back("https://hardcoregames.azurewebsites.net/wp-admin/");

必须在某个函数内。

您可以在编译时使用以下命令设置vector的值:

std::vector<std::string> sites = { site1, site2 };

正如您在评论中指出的那样,这是一个变量初始化。

或者,您可以将push_back语句移到main函数中,然后在程序启动时运行它 - 您甚至可以全力以赴并摆脱全局变量以支持本地变量(这将是一个非常好的设计选择.. .)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM