gpt4 book ai didi

c++ - 尝试编写备份程序 (C++) 时,类型 ‘int(const char*)’ 和 ‘const char [11]’ 到二进制 ‘operator<<’ 的无效操作数

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:30 24 4
gpt4 key购买 nike

在回答这个很可能很明显的问题之前,请记住我是一个仍在尝试学习 C++ 的普通程序员...如果有任何明显的错误,请对我宽容点。

所以我正在尝试编写一个程序来创建运算符(operator)想要的任何文件/目录的备份,但我不断收到奇怪的错误。我曾尝试使用 + 而不是 << (我知道菜鸟的举动,但你永远不知道)。我也尝试过设置 std::sting 但出现了类似的错误(我几乎尝试了所有方法,但当然不是所有方法,因为我还没有修复它)。

代码如下:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std ;
int main()
{
char name ;
char named ;
std::cout << "This program will create a backup of whatever directory you would like. Please enter what you would like the backup to be named: "
std::cin >> name ;
std::cout << "Now please enter what directory you wold like to be backed up (Example: /bin/bash): " ;
std::cin >> named ;
system << "tar -zcvf " << name << "tar.gz " << named ;
return 0 ;
}

这是我不断收到的错误:

backup.cpp: In function ‘int main()’:
backup.cpp:12:12: error: invalid operands of types ‘int(const char*)’ and ‘const char [11]’ to binary ‘operator<<’
system << "tar -zcvf " << name << "tar.gz " << named ;

(我知道我应该添加休息时间,但在这一点上我已经过去了{暂时})

感谢所有 awnsered.. 非常简单的错误和修复,但我发现这是最好的学习方式。

最佳答案

你的程序有多个问题。

  • 在其中一行中缺少 ;
  • 您需要使用 std::getline读取字符串。
  • 您的namenamed 变量应该是strings不是字符。
  • std::system是一个接受命令行字符串参数的函数。

例如:

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
string name;
string named;
std::cout << "This program will create a backup of whatever directory you would like."
"Please enter what you would like the backup to be named: ";
std::getline(cin, name);
std::cout << "Now please enter what directory you wold like to be backed up"
" (Example: /bin/bash): ";
std::getline(cin, named);
std::system(("tar -zcvf " + name + "tar.gz " + named).c_str());
return 0;
}

关于c++ - 尝试编写备份程序 (C++) 时,类型 ‘int(const char*)’ 和 ‘const char [11]’ 到二进制 ‘operator<<’ 的无效操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44081688/

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