提问者:小点点

理解C++字符串文字的困难


下面的代码是我的程序的一部分。它不执行,它说C++禁止将字符串常量转换为字符*。我如何修复这个错误?

#include <cstring>
#include <iostream>
using namespace std;

class Bank
{
private:
    char name[20];
    int acc;
    float amount;

public:
    Bank()
    {
        strcpy("",name);
        acc = 0;
        amount = 0;
    }

    void open(int a)
    {
        cout << "Enter name:";
        cin >> name;
        acc = a;
        cout << "Enter Amount" << endl;
        cin >> amount;
    }

    void edit(int flag, float a);
};

int main()
{
    Bank B[10];
}  

共1个答案

匿名用户

std::strcpy(“”,name,“”);需要为std::strcpy(name,“”);

尽管撇开std::stringname的更好类型(未来的英国国王Charles Mountbatten-Windsor不能成为客户)不谈,但

name[0] = 0;

就足够了。

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(理解|c++|字符串|文字|困难)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?