首页 > 解决方案 > 我有点问题,我似乎可以理解这个错误

问题描述

我正在创建一个程序,其中我需要登录一个帐户并查看余额和到期日,但是在我运行它时发生了一些错误。这是我遇到的错误:[Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string}' through '...'

我尝试在网上搜索如何修复,但仍然无法正常工作

这是我的代码:

#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;

}

标签: c++dev-c++

解决方案


该代码在 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 的评论。


推荐阅读