C++ 在C++;哎呀
在我的计算机科学课上,我的头文件中有一个枚举值,我在get和set函数方面遇到了问题。我对C++很陌生。这是我的头文件的一部分:C++ 在C++;哎呀,c++,visual-studio,oop,enums,C++,Visual Studio,Oop,Enums,在我的计算机科学课上,我的头文件中有一个枚举值,我在get和set函数方面遇到了问题。我对C++很陌生。这是我的头文件的一部分: enum class SchoolType { Elementary = 1, Secondary = 2 }; class School : public Institution { public: // There are a couple more values here SchoolType GetSchoo
enum class SchoolType {
Elementary = 1,
Secondary = 2
};
class School : public Institution
{
public:
// There are a couple more values here
SchoolType GetSchoolType();
void SetSchoolType(SchoolType typeSchool);
};
这是我的.cpp文件的一部分:
SchoolType GetTypeSchool()
{
return this->_typeSchool;
}
void SetTypeSchool(SchoolType typeSchool)
{
}
但是“this”带来了一个错误,并表示“this”只能在非静态成员函数中使用。我怎样才能使该函数工作?我的计算机老师告诉我,这就是我应该如何编写get函数的方法,但我仍然不明白,标题中是否有错误?对于.cpp文件,您应该:
SchoolType School::GetSchoolType() {
return this->_typeSchool;
}
void School::SetSchoolType(SchoolType typeSchool) {
// Insert code here...
}
<>基本上,在C++中,当定义成员函数时,需要指定函数是什么(在这种情况下是“代码>学校< /代码>”的一个类,或者编译器不把它看作是任何类的一部分。您还需要保持方法名称的一致性(GetSchoolType
vsGetTypeSchool
)