【发布时间】:2020-11-01 09:52:48
【问题描述】:
我正在尝试使用 C++ 进行密码提示。我的代码如下:
#include <iostream>
#include <string>
std::string operator * (std::string a, unsigned int b) {
std::string output = "";
while (b--) {
output += a;
}
return output;
}
int main(){
std::string pword;
std::string user;
std::cout << "Enter username: ";
std::cin >> user;
std::cout << "Enter password for user '" << user << "': ";
std::cin >> pword;
std::string pword_asterix = ("*") * pword.length(); // ERROR
std::clog << "Noted user '" << user << "' and password '" << pword_asterix << "'.";
但我从 Visual Studio Code 收到以下错误:
表达式必须具有算术或无范围枚举类型(第 17 行)
我该怎么办?
【问题讨论】:
-
你到底想在这里做什么:
std::string pword_asterix = ("*") * pword.length();? -
std::string pword_asterix = std::string("*") * pword.length(); -
@πάνταῥεῖ 我试图将一个字符串相乘。