C++ LINK 问题

来源:百度知道 编辑:UC知道 时间:2024/05/13 06:10:45
#include<iostream.h>
int i=0;
class student
{public:
float score[100];

void scoretotalcount(float s)
{score[i]=s;i++;
total+=s;count++;
cout<<"total="<<total<<'\t'<<"count="<<count<<endl<<"*************************"<<endl;};
//float sun(){return total;};
//float average(){return total/count;};
static float total,count;
};

void main()
{float cs;
student aclass;
for(;;)
{
cout<<"score=";
cin>>cs;

if(cs<0) break;
else aclass.scoretotalcount(cs);

};

}

执行程序出现:
--------------------Configuration: student - Win32 Debug--------------------
Compiling...
student.cpp
Linking...
student.obj : error LNK2001: unresolved external symbol "public: static float student::count" (?count@stu

去掉 static float total,count; 里面的 static

如果要 声明 为 static,可以抽出来作 全局量,把 static float total,count; 放到int i=0; 下面。

全局量 默认 是 static。

静态数据成员在类外定义并初始化
……
//float average(){return total/count;};
static float total,count;
};
float student::total=0;
float student::count=0;