c++ 问题,新手求教,
#include<iostream.h>classStudent{staticfloattotal;staticintcount;floatscore;public:vo...
#include<iostream.h>
class Student{
static float total;
static int count;
float score;
public:
void scoretotalcount(float s);
static float sum();
static float average();
};
void Student::scoretotalcount(float s){total=total+s;count=count+1;}
float Student::sum(){return total;}
float Student::average(){return total/count;}
void main()
{ int n;
cin>>n;
Student x;
for(int i=0;i<n;i++){float h;cin>>h;;x.scoretotalcount(h);}
cout<<x.sum()<<ends<<x.average()<<endl;}
Build时出现错误
78.obj : error LNK2001: unresolved external symbol "private: static int Student::count" (?count@Student@@0HA)
78.obj : error LNK2001: unresolved external symbol "private: static float Student::total" (?total@Student@@0MA)
Debug/9995.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错. 为什么会这样,哪里错了??
题目要类里有那2个静态成员, 在这个基础上,怎么才能解决那错误吖??? 展开
class Student{
static float total;
static int count;
float score;
public:
void scoretotalcount(float s);
static float sum();
static float average();
};
void Student::scoretotalcount(float s){total=total+s;count=count+1;}
float Student::sum(){return total;}
float Student::average(){return total/count;}
void main()
{ int n;
cin>>n;
Student x;
for(int i=0;i<n;i++){float h;cin>>h;;x.scoretotalcount(h);}
cout<<x.sum()<<ends<<x.average()<<endl;}
Build时出现错误
78.obj : error LNK2001: unresolved external symbol "private: static int Student::count" (?count@Student@@0HA)
78.obj : error LNK2001: unresolved external symbol "private: static float Student::total" (?total@Student@@0MA)
Debug/9995.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错. 为什么会这样,哪里错了??
题目要类里有那2个静态成员, 在这个基础上,怎么才能解决那错误吖??? 展开
5个回答
展开全部
错误处:静态数据成员必须在【类外】进行初始化!!!!
修改程序如下:
#include<iostream.h>
class Student
{
static float total;
static int count;
float score;
public:
Student(){ score = 0;} /////////////构造函数初始化score
void scoretotalcount(float s);
static float sum();
static float average();
};
float Student::total=0; /////////////////类外初始化
int Student::count=0; /////////////////类外初始化
void Student::scoretotalcount(float s)
{
total=total+s;count=count+1;
}
float Student::sum()
{
return total;
}
float Student::average()
{
return total/count;
}
void main()
{
int n;
cin>>n;
Student x;
for(int i=0;i<n;i++)
{
float h;
cin>>h;
x.scoretotalcount(h);
}
cout<<x.sum()<<ends<<x.average()<<endl;
}
修改程序如下:
#include<iostream.h>
class Student
{
static float total;
static int count;
float score;
public:
Student(){ score = 0;} /////////////构造函数初始化score
void scoretotalcount(float s);
static float sum();
static float average();
};
float Student::total=0; /////////////////类外初始化
int Student::count=0; /////////////////类外初始化
void Student::scoretotalcount(float s)
{
total=total+s;count=count+1;
}
float Student::sum()
{
return total;
}
float Student::average()
{
return total/count;
}
void main()
{
int n;
cin>>n;
Student x;
for(int i=0;i<n;i++)
{
float h;
cin>>h;
x.scoretotalcount(h);
}
cout<<x.sum()<<ends<<x.average()<<endl;
}
展开全部
你没写构造函数啊。而且两个成员数据为什么用静态变量。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据成员没有初始化
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ends,写错了,是endl,还有就是静态的成员变量,是不能变的,static是多余的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
就是判断你输入的是不是字符串结束符,如果是认为输入完毕(即if条件成立),则break跳出while循环。如果把break放到if模块后面,那么你输入一个字符就会跳出循环,导致程序结束
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询