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

无法解析的外部符号 "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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog