一弦一柱
2017-05-24 12:23
采纳率: 100%
浏览 1.3k
已采纳

无法解析的外部符号 "private: static float student::z m e"

#include
#include
using namespace std;

class student{
string p;
int num;
float math, english;
static float z, m, e;
public:
student(string s, int n, float a, float b)
{
p = s, num = n, math = a, english = b;
z = m = e = 0;
}
student(){}
void input();
void output();
float sum();
static void outputsum();
float average();
~student(){}
};

void student::input()
{
cout << "输入姓名:";
string s;
cin.sync();
getline(cin, s);
cout << "输入学号:";
int n;
cin >> n;
float x, b;
cout << "输入数学成绩:";
cin >> x;
cout << "输入英语成绩:";
cin >> b;
student(s, n, x, b);
}

void student::output()
{
cout << "姓名:" << p << "\n学号:" << num << endl
<< "数学成绩:" << math << endl << "英语成绩:" << english
<< endl << "总成绩:" << this->sum() << endl << "平均成绩:" << this->average() << endl;
}

float student::sum()
{
z += math + english, m += math, e += english;
return math + english;
}

void student::outputsum()
{
cout << "所有学生的总成绩:" << z << '\n' << "所有学生的数学成绩:" << m
<< endl << "所有学生的英语成绩:" << e << endl;
}

float student::average()
{
return (this->sum()) / 2;
}

int main()
{
int n;
cout << "students' number:";
cin >> n;
student *s = new student[n];
if (NULL == s)
return 1;
for (int i = 0; i < n; i++)
s[i].input();
for (int i = 0; i < n; i++)
s[i].output();
s->outputsum();
delete[]s;
system("pause");
return 0;
}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 战在春秋 2017-05-24 13:00
    已采纳

    类的静态成员属于整个类的而不是某个对象。

     静态成员变量必须初始化,而且只能在类体外进行。
    
    

    对这个题目来说,修改两处:

    1

    student(string s, int n, float a, float b)
        {
            p = s, num = n, math = a, english = b;
            //z = m = e = 0;  //注释或删除这行。
        }
    

    2

    //在main函数之前加入静态成员初始化语句
    float student :: z = 0.0f;
    float student :: m = 0.0f;
    float student :: e = 0.0f;
    
    int main()
    {
    
    

    如果想进一步深入学习,可以参考:
    http://c.biancheng.net/cpp/biancheng/view/209.html

    用心回答每个问题,如果有帮助,请采纳答案好吗,谢谢~~

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题