首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

新手,内附代码

2012-05-21 
新手求助,内附代码#include iostream#include cstdlib#include list#include functional#include

新手求助,内附代码
#include <iostream>
#include <cstdlib>
#include <list>
#include <functional>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
class student
{
private:
  double math;
  double english;
  double sum;
  fstream c1;
public:
  student(){}
  void setstudent(double m,double e)
  {
  math=m;
  english=e;
  }
  double getsum()
  {
  sum=math+english;
  return sum;
  }
  void display()
  {
  cout<<math<<" "<<english<<" "<<getsum()<<endl;
  }
  char *Filename(string &file);
  void printStudent(string &file);
};
void student::printStudent(string &file)
{
  c1.clear();
  c1.open(Filename(file),fstream::out | fstream::app);
  if(!c1)
  {
  cerr<<"Can not open file."<<endl;
  system("pause");
  exit(1);
  }
  c1<<math<<" "<<english
  <<" "<<getsum()<<endl;
  c1.clear();
  c1.close();
}
char* student::Filename(string &file)
{
  char *cp;
  cp=new char[file.size()+1];
  char *txt=".txt";
  strcpy(cp,file.c_str());
  strcat(cp,txt);
  return cp;
  delete cp;
}

class Lstudent
{
private:
  list<student> L;
public:
  void list_add(student &_s)
  {
  L.push_back(_s);
  }
  static bool cmp_sum(student &_s1,student &_s2)
  {
  return _s1.getsum() > _s2.getsum();
  }
  void list_sort()
  {
  L.sort(cmp_sum);
  }
  static void L_display(student &_s)
  {
  _s.display();
  }
  void LL_display()
  {
  for_each(L.begin(),L.end(),L_display);
  }
  void L_print(string &file)
{
  list<student>::iterator it;
  for(it=L.begin();it!=L.end();++it)
  (*it).printStudent(file);//调用函数 
  system("pause");
}
};

int main()
{
  student s1;
  Lstudent l1;
  string filename("0012");
  s1.setstudent(35.5,50.5);
  l1.list_add(s1);
  s1.setstudent(50.2,85.6);
  l1.list_add(s1);
  s1.setstudent(12,30);
  l1.list_add(s1);
  l1.list_sort();
  l1.LL_display();
  l1.L_print(filename);
  system("pause");
  return 0;
}

在Dev-c++ 4.9.9.2中提示Build error。
应该是文件流函数出现的问题,但不知道怎么改,各位能不能帮帮忙。。

[解决办法]

探讨
引用:
C/C++ code
char* student::Filename(string &amp;amp;file)
{
char *cp;
cp=new char[file.size()+1];
char *txt=".txt";
strcpy(cp,file.c_str());
strcat(cp,txt);
return cp;
……

我主要是想……


热点排行