【发布时间】:2014-10-01 11:15:44
【问题描述】:
由于 'std::string' has no member named 'username' 错误,我的代码无法编译。我正在使用代码::块。我猜这个问题是因为我试图将一个字符串分配给一个类,有人可以帮忙吗?
#include <iostream>
#include <string>
using namespace std;
class userx
{
public:
string username;
string password;
};
int main()
{
userx X, Y;
X.username = "X";
X.password = "1234";
Y.username = "Y";
Y.password = "5678";
string a;
string b;
cout << "What is your name?" << endl;
cin >> a;
if (a.username == a)
{
cout << "What is your password?" << endl;
cin >> b
if (a.password == b)
{
cout << "Password Accepted" << endl;
};
};
};
【问题讨论】:
-
如果你使用正确的变量名,这个错误可能会被避免。 X、Y、a 和 b 完全没有告诉你变量代表什么。