简体   繁体   中英

C++ simple class

I started learning C++, classes, objects, structures and more, but I'm having some problems with this.

#include <iostream>

using namespace std;

class Owner
{
    public:
        // Getters
        string GetName(){return info.name;}
        int GetAge(){return info.age;}
        short int GetGender(){return info.gender;}

        // Setters
        void SetName(string value){info.name = value;}
        void SetAge(int value){info.age = value;}
        void SetGender(short int value){info.gender = value;}
    private:
        struct info
        {
            string name;
            int age;
            short int gender;
        };
};

class Pet
{
    public:
        // Getters
        string GetName(){return info.name;}
        int GetAge(){return info.age;}
        short int GetGender(){return info.gender;}

        // Setters
        void SetName(string value){info.name = value;}
        void SetAge(int value){info.age = value;}
        void SetGender(short int value){info.gender = value;}
    private:
        struct info
        {
            string name;
            int age;
            short int gender;
        }
};


int main()
{

    // Creating object ...

    cout << "qq" << endl;



    return 0;
}

But I get these errors when I try to compile it:

In member function 'std::string Owner::GetName()':|
main.cpp|9|error: expected primary-expression before '.' token|
In member function 'int Owner::GetAge()':|
main.cpp|10|error: expected primary-expression before '.' token|
In member function 'short int Owner::GetGender()':|
main.cpp|11|error: expected primary-expression before '.' token|
In member function 'void Owner::SetName(std::string)':|
main.cpp|14|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetAge(int)':|
main.cpp|15|error: expected unqualified-id before '.' token|
In member function 'void Owner::SetGender(short int)':|
main.cpp|16|error: expected unqualified-id before '.' token|
main.cpp|45|error: expected unqualified-id before '}' token|
In member function 'std::string Pet::GetName()':|
main.cpp|30|error: expected primary-expression before '.' token|
In member function 'int Pet::GetAge()':|
main.cpp|31|error: expected primary-expression before '.' token|
In member function 'short int Pet::GetGender()':|
main.cpp|32|error: expected primary-expression before '.' token|
In member function 'void Pet::SetName(std::string)':|
main.cpp|35|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetAge(int)':|
main.cpp|36|error: expected unqualified-id before '.' token|
In member function 'void Pet::SetGender(short int)':|
main.cpp|37|error: expected unqualified-id before '.' token|
||=== Build finished: 13 errors, 0 warnings ===|

Why does it give me so many errors?

I don't know why, because it is obvious that, for example,

 string GetName()
 {
     return info.name;
 }

returns a string, from the structure info.name

I'm using CodeBlocks.

You're declaring the struct as a type ( Owner.info ) instead of as a member ( this->info ). You probably want this:

struct OwnerInfo
{
    string name;
    int age;
    short int gender;
};

class Owner {
    // stuff..
private:
    OwnerInfo info;
};

Or, the more reasonable version would be just having them there directly instead of inside a pointless struct :

class Owner {
    // stuff..
private:
    string name;
    int age;
    short int gender;
};

You're misunderstanding the syntax of the struct keyword, furthermore the actual member variable has to be declared before the member functions accessing it. So change your class declarations to something like

class Owner
{
private:
    struct
    {
        string name;
        int age;
        short int gender;
    } info;

public:
    // Getters
    string GetName(){return info.name;}
    int GetAge(){return info.age;}
    short int GetGender(){return info.gender;}

    // Setters
    void SetName(string value){info.name = value;}
    void SetAge(int value){info.age = value;}
    void SetGender(short int value){info.gender = value;}
};

The declaration:

private:
    struct info
    {
        string name;
        int age;
        short int gender;
    };

... defines the layout of a nested structure type info , much like the definition of the entire class does. However, info is now a type nested within Owner, not an instance of that type that is a member of Owner. Instead, try naming the struct Info, then declaring Info info = new Info(); in the private section of Owner.

Well you created a struct and therefor told your compiler "info" is a typ with the following attributes...

You need to declare a variable of the type "info".

info personalInfo; 

Declare it as class member and you can create your Get-er and Set-er.

string GetName(){return personalInfo.name;}

More Information

You should create a object of struct info. You just cannot access a field of a struct directly without creating an object.. Why do you need the struct at all? try this

class Owner
{
    private:
            string name;
            int age;
            short int gender;

    public:
        // Getters
        string GetName(){return this->name;}
        int GetAge(){return this->age;}
        short int GetGender(){return this->gender;}

        // Setters
        void SetName(string value){this->name = value;}
        void SetAge(int value){this->age = value;}
        void SetGender(short int value){this->gender = value;}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM