例子 vc6.0 编译通过

//studentc.h

 

#ifndef STUDENTC_H
#define STUDENTC_H
#include <iostream>
#include <valarray>

#include <string>

//#include <string>
//#include <string>

//using namespace std;
class Student
{
private:
   
    typedef std::valarray <double> ArrayDb;//定义一个类型
    std::string name;//string类的name 对象
    ArrayDb scores;//arraydb类的scores对象
    std::ostream & arr_out(std::ostream & os)const;//定义了个输出
public:
    //下面为几个构造函数:
    Student():name("Null name"),scores(){}
    Student(const std::string & s):name(s),scores(){}
    explicit Student(int n):name("Nully"),scores(n){}
    Student(const std::string & s,int n)
        :name(s),scores(n){}
    Student(const std::string & s,const ArrayDb & a)
        :name(s),scores(a){}
    Student(const char *str,const double *pd,int n)
        :name(str),scores(pd,n){}
    ~Student(){}
    double Average()const;//求平均成绩
    const std::string & Name()const;
    double & operator[](int i);
    double operator[](int i)const;
    //friends
    //input
 //friend std::istream & operator >>(std::istream & is,Student & stu);
    friend std::istream & operator >>(std::istream & is,Student & stu)
 {
  is>>stu.name;
  return is;
 }
 
 //friend std::istream & Mygetline(std::istream & is,Student & stu);
    friend std::istream & Mygetline(std::istream & is,Student & stu)
 {
        //getline(is, stu.name);
  std::getline(is, stu.name);

  return is;
 }
 
    //output
 //friend std::ostream & operator <<(std::ostream & os,const Student & stu);
    friend std::ostream & operator <<(std::ostream & os,const Student & stu)
{
    os <<"Scores for" <<stu.name <<":\n";
    stu.arr_out(os);//use private method for scores 
    return os;
}
};
#endif

 

//studentc.cpp

#include <string>
#include "studentc.h"
using namespace std;
//public methods
double Student::Average()const
{
    if(scores.size() >0)
        return scores.sum()/scores.size();
    else
        return 0;
}
const string & Student::Name()const
{
    return name;
}
double & Student::operator [](int i)
{
    return scores[i];
}
double Student::operator [](int i)const
{
    return scores[i];
}
//private method
ostream & Student::arr_out(ostream & os)const
{
    int i;
    int lim=scores.size();
    if(lim >0)
    {
        for(i=0;i <lim;i++)
        {
            os << scores[i] <<" ";
            if(i%5==4)
                os << endl;
        }
        if(i%5!=0)
            os << endl;
    }
    else
        os <<"empty array";
    return os;
}
//friends //VC6.0 友元函数的定义要在类中

/*
//use string version of operator >>()
istream & operator >>(istream & is,Student& stu)
{
    is >> stu.name;
    return is;
}
//use string friend getline(ostream &,const string &)
istream& Mygetline(istream & is,Student& stu)
{
    getline(is, stu.name);
    return is;
}
//use string version of operator <<()
ostream & operator <<(ostream & os,const Student & stu)
{
    os <<"Scores for" <<stu.name <<":\n";
    stu.arr_out(os);//use private method for scores 
    return os;
}
*/

 

//mian.cpp

#include "stdafx.h"

#include <iostream>

#include "studentc.h"


using std::cin;
using std::cout;
using std::endl;
//using namespace std;

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;)
        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 <<"Plaese enter the student 's name:";
    Mygetline(cin,sa);
    cout <<"Please enter" <<n <<"quiz scores:\n";
    for(int i=0;i <n;i++)
        cin >>sa[i];
    while(cin.get()!= '\n ')
        continue;
}

 

fatal error C1010: unexpected end of file while looking for precompiled header directive

解决:在.cpp文件开头添加包含文件stdafx.h。 #include"stdafx.h"  或 在菜单Project->Settings->C/C++->Precompile Header,设置为第一项:Not using precompile headers。

 

error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

error C2039: 'getline' : is not a member of 'std'

error C2065: 'getline' : undeclared identifier

加 #include <string> 解决

 

 getlie这个标识名有命名空间 std中定义的。如果要使用

加上using namespace std;

或  std::getline

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值