新微赢技术网
标题:
[求助]郁闷阿!这个程序怎么编译不了?
[打印本页]
作者:
梦的放肆
时间:
2009-11-4 00:04
标题:
[求助]郁闷阿!这个程序怎么编译不了?
//studenti.h
#ifndef STUDENTI_H_
#define STUDENTI_H_
#include<iostream>
#include<string>
#include<valarray>
class Student:private std::string,private std::valarray<double>
{
private:
typedef std::valarray<double> ArrayDb;
std::ostream & arr_out(std::ostream & os)const;
public:
Student():std::string("Null Student"),ArrayDb() {}
Student(const std::string & s)
:std::string(s),ArrayDb() {}
explicit Student(int n)
:std::string("Nully"),ArrayDb(n) {}
Student(const std::string & s,int n)
:std::string(s),ArrayDb(n) {}
Student(const std::string & s,const ArrayDb & a)
:std::string(s),ArrayDb(a) {}
Student(const char * str,const double * pd,int n)
:std::string(str),ArrayDb(pd,n) {}
~Student() {}
double Average()const;
const std::string & Name()const;
double & operator[](int i);
double operator[](int i)const;
friend std::istream & operator>>(std::istream & is,
Student & stu);
friend std::istream & getline(std::istream & is,
Student & stu);
friend std::ostream & operator<<(std::ostream & os,
const Student & stu);
};
#endif
//student.cpp
#include"studenti.h"
using std::ostream;
using std::endl;
using std::istream;
using std::string;
double Student::Average()const
{
if(ArrayDb::size()>0)
return ArrayDb::sum()/ArrayDb::size();
else
return 0;
}
const string & Student::Name()const
{
return (const string &)*this;
}
double & Student::operator [](int i)
{
return ArrayDb::operator[](i);
}
double Student::operator [](int i)const
{
return ArrayDb::operator[](i);
}
ostream & Student::arr_out(ostream & os)const
{
int i;
int lim=ArrayDb::size();
if(lim>0)
{
for(i=0;i<lim;i++)
{
os<<ArrayDb::operator[](i)<<" ";
if(i%5==4)
os<<endl;
}
if(i%5!=0)
os<<endl;
}
else
os<<" empty array ";
return os;
}
istream & operator>>(istream & is,Student & stu)
{
is>>(string &)stu;
return is;
}
istream & getline(istream & is,Student & stu)
{
getline(is,(string &)stu);
return is;
}
ostream & operator<<(ostream & os,const Student & stu)
{
os<<"Scores for "<<(const string &)stu<<":\n";
stu.arr_out(os);
return os;
}
//usestu.cpp
#include<iostream>
#include"studenti.h"
using std::cin;
using std::cout;
using std::endl;
void set(Student & sa, int n);
const int pupils=3;
const int quizzes=5;
int main()
{
Student ada[pupils]={Student(quizzes),Student(quizzes),Student(quizzes)};
int i;
for(i=0;i<pupils;i++)
set(ada[i],quizzes);
cout<<"\nStudent List:\n";
for(i=0;i<pupils;i++)
cout<<ada[i].Name()<<endl;
cout<<"\nResults:";
for(i=0;i<pupils;i++)
{
cout<<endl<<ada[i];
cout<<"average: "<<ada[i].Average()<<endl;
}
cout<<"Done.\n";
return 0;
}
void set(Student & sa,int n)
{
cout<<"Please enter the student's name: ";
getline(cin,sa);
cout<<"Please enter "<<n<<" quiz scores:\n";
for(int i=0;i<n;i++)
cin>>sa[i];
while(cin.get()!='\n')
continue;
}
这个程序和我前两天求教的一个程序基本类似,只是前面的用的是包含的类继承,这次是用的私有继承,还是完成输入3个学生的各5门功课成绩,求其各自平均分后输出,我在VC++6.0上编译出现如下错误:
Compiling...
use_stui.cpp
d:\vc源程序练习\use_stui\studenti.h(14) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(15) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(17) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(19) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(21) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
d:\vc源程序练习\use_stui\studenti.h(23) : error C2614: 'Student' : illegal member initialization: 'string' is not a base or member
Error executing cl.exe.
use_stui.obj - 6 error(s), 0 warning(s)
难道编译器不支持私有继承?请教各位了
作者:
顺其自然
时间:
2009-11-4 00:04
大哥,先把注释挂上去再说,这么长的程序没有注释谁有耐心去看啊
作者:
jane
时间:
2009-11-4 00:04
函数实现和声名之间有错误
很难看出来啊
作者:
孤独者
时间:
2009-11-4 00:04
我有点晕了!
作者:
假tp老tp练
时间:
2009-11-4 00:04
以前我贴程序也有这个习惯....那就是没有注释!!!!!!!!!!!!!!
作者:
第三滴眼泪$
时间:
2009-11-4 00:04
太长了
自己用Debug追踪看看.
作者:
我是哈密瓜耶
时间:
2009-11-4 00:04
用上using namespace std;
这个程序就简单多了.
作者:
!今生无缘!
时间:
2009-11-4 00:04
头文件还是少用命名空间的好
欢迎光临 新微赢技术网 (http://bbs.weiying.cn/)
Powered by Discuz! X3.2