我有点问题,我似乎可以理解这个错误

I have a bit of a problem, I can seem to understand this error

我正在创建一个程序,其中我需要登录一个帐户并查看余额和到期日,但是当我 运行 它时发生了一些错误。 这是我遇到的错误: [错误] 无法通过“...”

传递不可平凡复制类型 'std::string {aka class std::basic_string}' 的对象

我尝试在线搜索修复方法,但仍然无效

这是我的代码:

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main() {
    string names[2] = { "MARK", "EMILY" }, Ddate[2] = { "10/04/19","10/06/19" };
    int bal[2] = { 4500,6500 }, id_num[2] = { 1810784, 1810783 }, pword[2] = { 117611,594356 }, idnum, pw,i = 0;

    char check_bal,temp;
    printf("WELCOME TO STUDENT BILLING SYSTEM \n");
    printf("Would you like to check your balance? Y/N\n");
    scanf( "%c", &check_bal);
    temp = toupper(check_bal);
    check_bal = temp;

    if (check_bal == 'Y') {
        printf("ENTER YOUR ID NUMBER: ");
        scanf("%i", &idnum);
        printf("ENTER YOUR PASSWORD: ");
        scanf("%i", &pw);
        while (true) {
            if (idnum == id_num[i])
                break;
            else
                continue;
        }
        while (true) {
            if (pw == pword[i])
                break;
            else
                continue;
        }
        printf("HI %s, your balance is %i and the DUE DATE is: %s \n", names[i],bal[i],Ddate[i]);
}
else
    system("EXIT");

system("PAUSE");
return 0;

}

代码在 g++ 中编译得很好,但输出垃圾。 使用 g++ -Wall 你会得到明智的警告:

warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘std::__cxx11::string’ {aka ‘std::__cxx11::basic_string<char>’} [-Wformat=]

这是 g++ 版本 8.3.0。

可以像这样挽救代码:

printf("HI %s, your balance is %i and the DUE DATE is: %s \n", names[i].c_str(),bal[i],Ddate[i].c_str());

但是请注意推荐使用c++风格io的评论。