我开始学习 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
我正在使用代码块。