C ++定义类成员结构并在成员函数中返回它 [英] C++ define class member struct and return it in a member function
问题描述
我的目标是像这样的课程:
My goal is a class like:
class UserInformation
{
public:
userInfo getInfo(int userId);
private:
struct userInfo
{
int repu, quesCount, ansCount;
};
userInfo infoStruct;
int date;
};
userInfo UserInformation::getInfo(int userId)
{
infoStruct.repu = 1000;
return infoStruct;
}
但是编译器给出了错误,它定义了公共函数 getInfo(int)
返回类型 userInfo
不是类型名称。
but the compiler gives error that in defintion of the public function getInfo(int)
the return type userInfo
is not a type name.
推荐答案
您需要更改 UserInformation
成员的顺序,并放入 struct UserInfo
上方声明 getInfo
。编译器抱怨说,它无法计算 getInfo
的签名,因为尚未看到其返回类型的定义。
You need to change the order of the members of UserInformation
and put struct UserInfo
above the declaration of getInfo
. The compiler complains that it can't work out the signature for getInfo
because it hasn't seen the definition of its return type yet.
此外,如果要从函数返回结构,则该结构的类型必须对调用者可见。因此,您还需要将结构公开
。
Also, if you are returning a struct from the function the type of the struct must be visible to the callers. So you need to make the struct public
as well.
这篇关于C ++定义类成员结构并在成员函数中返回它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!