【发布时间】:2014-03-01 00:31:48
【问题描述】:
#include <iostream>
using namespace std;
struct AccountInfo
{
int Acnumber;
char ID;
};
class Account
{
public:
AccountInfo Operation(void)
{
AccountInfo s;
s.ID = 'M';
return s;
}
};
int main()
{
Account a;
AccountInfo s = a.Operation();
cout << s.ID << endl;
return 0;
}
我正在尝试返回给定给类方法内部结构的值。代码编译时出现错误。我尝试使用一个对象,它编译成功,但不输出任何东西。
我的代码有什么问题?
【问题讨论】:
-
您“尝试使用对象”是什么意思?
-
我定义了一个类并在 Account 类中创建了该类的对象,而不是 struct。
-
请您出示该代码。
-
#include
使用命名空间标准;类 AccountInfo { public: int Acnumber;字符 ID; };类 Account { public: AccountInfo Operation(void) { AccountInfo s; s.ID = 'M';返回 s; } }; int main() { 帐户 a; AccountInfo s = a.Operation(); cout -
当我运行您的代码时,编译器告诉我您在类定义后缺少分号,并且您尝试从
AccountInfo对象访问的数据成员是私有的。要解决此问题,请在Account类的末尾大括号处添加分号;,并在AccountInfo的数据成员上添加public:。要么这样做,要么将AccountInfo设为struct而不是class,从而使其成员默认可公开访问。