简体   繁体   English

C ++简单类

[英]C++ simple class

I started learning C++, classes, objects, structures and more, but I'm having some problems with this. 我开始学习C ++,类,对象,结构等,但是与此同时我遇到了一些问题。

#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 从结构info.name返回一个字符串

I'm using CodeBlocks. 我正在使用CodeBlocks。

You're declaring the struct as a type ( Owner.info ) instead of as a member ( this->info ). 您将结构声明为类型( Owner.info ),而不是成员( 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 : 或者,更合理的版本是将它们直接放在那里,而不是放在无意义的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. 您误解了struct关键字的语法,此外,必须成员函数访问它之前声明实际的成员变量。 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. ...定义嵌套结构类型info的布局,就像整个类的定义一样。 However, info is now a type nested within Owner, not an instance of that type that is a member of Owner. 但是, info现在是嵌套在Owner中的一种类型,而不是属于Owner成员的该类型的实例。 Instead, try naming the struct Info, then declaring Info info = new Info(); 相反,尝试命名结构Info,然后声明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... 好了,您创建了一个结构,因此告诉编译器“ info”是具有以下属性的类型:

You need to declare a variable of the type "info". 您需要声明“ info”类型的变量。

info personalInfo; 

Declare it as class member and you can create your Get-er and Set-er. 将其声明为班级成员,您可以创建Get-er和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;}
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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