0

我开始学习 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;
}

但是当我尝试编译它时出现这些错误:

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 ===|

为什么它给我这么多错误?

我不知道为什么,因为很明显,例如,

 string GetName()
 {
     return info.name;
 }

从结构中返回一个字符串info.name

我正在使用代码块。

4

5 回答 5

2

您将结构声明为类型 ( Owner.info) 而不是成员 ( this->info)。你可能想要这个:

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

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

或者,更合理的版本是直接将它们放在那里而不是放在无意义的内部struct

class Owner {
    // stuff..
private:
    string name;
    int age;
    short int gender;
};
于 2012-08-30T19:35:08.870 回答
1

您误解了 struct 关键字的语法,此外,必须在成员函数访问它之前声明实际的成员变量。因此,将您的类声明更改为类似

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;}
};
于 2012-08-30T19:35:30.807 回答
0

声明:

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

... 定义嵌套结构类型的布局info,就像整个类的定义一样。但是,info现在是嵌套在 Owner 中的类型,而不是属于 Owner 成员的该类型的实例。相反,尝试命名结构信息,然后Info info = new Info();在所有者的私有部分中声明。

于 2012-08-30T19:34:58.887 回答
0

好吧,您创建了一个结构,因此告诉您的编译器“信息”是具有以下属性的类型...

您需要声明一个“info”类型的变量。

info personalInfo; 

将其声明为类成员,您可以创建您的 Get-er 和 Set-er。

string GetName(){return personalInfo.name;}

更多信息

于 2012-08-30T19:35:25.063 回答
0

您应该创建一个结构信息对象。你不能在不创建对象的情况下直接访问结构的字段。为什么你根本需要结构?试试这个

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;}
};
于 2012-08-30T19:38:13.193 回答