赋初值函数还要求带返回值么?
#include <iostream> #include <stdlib.h> #include <string> using namespace std; class Student { public: void setName(string _name) { m_strName = _name; } string getName() { return m_strName; } void setGender(string _gender) { m_strGender=_gender; } string getGender() { return m_strGender; } int getScore() { return m_iScore; } int initScore() { m_iScore = 0; } void study(int _score) { m_iScore+=_score; //m_iScore=m_iScore+_score; } private: string m_strName; string m_strGender; int m_iScore; }; int main(void) { Student stu; stu.initScore(); stu.setName("wsq"); stu.setGender("男"); stu.study(8); stu.study(9); cout<<stu.getName()<<","<<stu.getGender()<<","<<stu.getScore()<<endl; system("pause"); return 0; }
提示错误 : error C4716: “Student::initScore”: 必须返回一个值
然后initScore()里面加retrun 0,调试成功。
老师视频用的VS2010,没加retrun 0也成功调试,我用的VS2012。哪位大神解释一下?