我的代碼哪里有問題
以下是有問題的代碼
2.cpp
# include <iostream>
# include<stdlib.h>
# include<string>
# include "Teacher.h"
using namespace std;
int main(void)
{
Teacher t1("Merry", 12, 150);
cout << t1.getName() << " " << t1.getAge() << " " << t1.getMax() << endl;
system("pause");
return 0;
}
Teacher.h
#include<iostream>
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string name = "Jim",int age = 1,int m = 100);
void setName(string name);
string getName();
void setAge(int age);
int getAge();
private:
string m_strName;
int m_iAge;
const int m_iMax;
};
2018-08-07
你的私有成員都沒有的得到有效的賦值傳遞,函數(shù)構(gòu)造里面都是把值默認(rèn)給了name,age,這些并不是你定義的變量,函數(shù)那些好像也沒實現(xiàn)。Teacher(string name = "? 默認(rèn)",int age = 1, int max = 100){m_strName = name;m_iAge = age, m_iMax = max;}如果還有錯,就把set和get函數(shù)實現(xiàn)了,比如int getAge(){return m_iAge;}? ? ? void setAge(int age){m_iAge = age;}類推,把name函數(shù)和max函數(shù)全部實現(xiàn),建議簡單的函數(shù)直接在Teacher.h文件里一并實現(xiàn),你也可以重新創(chuàng)建一個.cpp但是在里面要導(dǎo)入.h頭文件
2018-08-04
m_iMax 沒有定義set 和 get