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;
}
but the compiler gives error that in defintion of the public function getInfo(int)
the return type userInfo
is not a type name.
1. Member functions inside the structure: Structures in C cannot have member functions inside a structure but Structures in C++ can have member functions along with data members.
Yes you can. In c++, class and struct are kind of similar. We can define not only structure inside a class, but also a class inside one. It is called inner class.
Yes, there is a clear answer: C++ struct can have member functions.
Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.
If the member function is public, then the return type must be publicly visible! Therefore, move the inner struct definition into the public
section.
Note also that it must be defined before the function that uses it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With