我开始学习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
我正在使用CodeBlocks。
答案 0 :(得分: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;
};
答案 1 :(得分: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;}
};
答案 2 :(得分:0)
宣言:
private:
struct info
{
string name;
int age;
short int gender;
};
...定义嵌套结构类型info
的布局,非常类似于整个类的定义。但是,info
现在是嵌套在所有者中的类型,而不是作为所有者成员的该类型的实例。相反,尝试命名struct Info,然后在Owner的私有部分声明Info info = new Info();
。
答案 3 :(得分:0)
你创建了一个结构,因此告诉你的编译器“info”是一个具有以下属性的类型...
您需要声明“info”类型的变量。
info personalInfo;
将其声明为类成员,您可以创建Get-er和Set-er。
string GetName(){return personalInfo.name;}
答案 4 :(得分:0)
您应该创建一个struct info对象。你不能在不创建对象的情况下直接访问结构的字段。为什么你需要结构?试试这个
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;}
};