新手求助,内附代码

Verg1L 2012-05-13 02:09:26
#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。
应该是文件流函数出现的问题,但不知道怎么改,各位能不能帮帮忙。。
...全文
90 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
小九 2012-05-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
引用 1 楼 的回复:
C/C++ code
char* student::Filename(string &amp;file)
{
char *cp;
cp=new char[file.size()+1];
char *txt=".txt";
strcpy(cp,file.c_str());
strcat(cp,txt);
return cp;
……

我主要是想……
[/Quote]

第一、cp你申请的空间小了吧。你想加进去四个字符,干嘛多申请了一个字节的空间。再者你那个delete没用了,因为先返回了。
第二、主函数里你加进去三个student,干嘛在一个student的基础上来回改呢。虽然list的push_back确实是拷贝的,所以你程序没问题,但是理解起来也还是不通顺的。
再者,你可以加些assert判断一下,就可以彻底定位哪里的代码出了问题。比方说在主函数的print前,比如在打开文件的前边后面。
Verg1L 2012-05-13
  • 打赏
  • 举报
回复
编译器: Default compiler
执行 g++.exe...
g++.exe "C:\Documents and Settings\Administrator\桌面\student\test.cpp" -o "C:\Documents and Settings\Administrator\桌面\student\test.exe" -I"E:\DEV-CPP\lib\gcc\mingw32\3.4.2\include" -I"E:\DEV-CPP\include\c++\3.4.2\backward" -I"E:\DEV-CPP\include\c++\3.4.2\mingw32" -I"E:\DEV-CPP\include\c++\3.4.2" -I"E:\DEV-CPP\include" -L"E:\DEV-CPP\Lib"
E:/DEV-CPP/include/c++/3.4.2/bits/ios_base.h: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
E:/DEV-CPP/include/c++/3.4.2/bits/stl_construct.h:81: instantiated from `void std::_Construct(_T1*, const _T2&) [with _T1 = student, _T2 = student]'
E:/DEV-CPP/include/c++/3.4.2/bits/stl_list.h:436: instantiated from `std::_List_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_node(const _Tp&) [with _Tp = student, _Alloc = std::allocator<student>]'
E:/DEV-CPP/include/c++/3.4.2/bits/stl_list.h:1161: instantiated from `void std::list<_Tp, _Alloc>::_M_insert(std::_List_iterator<_Tp>, const _Tp&) [with _Tp = student, _Alloc = std::allocator<student>]'
E:/DEV-CPP/include/c++/3.4.2/bits/stl_list.h:783: instantiated from `void std::list<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = student, _Alloc = std::allocator<student>]'
C:\Documents and Settings\Administrator\桌面\student\test.cpp:68: instantiated from here
E:/DEV-CPP/include/c++/3.4.2/bits/ios_base.h:738: error: `std::ios_base::ios_base(const std::ios_base&)' is private
E:/DEV-CPP/include/c++/3.4.2/bits/stl_construct.h:81: error: within this context

E:/DEV-CPP/include/c++/3.4.2/streambuf: In copy constructor `std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)':
E:/DEV-CPP/include/c++/3.4.2/streambuf:769: error: `std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
E:/DEV-CPP/include/c++/3.4.2/bits/stl_construct.h:81: error: within this context

执行结束

这个是Dev-c++的编译日志
Verg1L 2012-05-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
C/C++ code
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;
……
[/Quote]
我主要是想把传入的string字符串转为"string+'.txt'"的字符数组。。
以前有个程序上也用同样的方法写过,没有报错。。。
screwzm 2012-05-13
  • 打赏
  • 举报
回复
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;
}

你的cp返回的时候已经释放了,再使用当然出错!永远不要返回指向局部作用域的指针变量!
其实可以直接这样:

void student::printStudent(string &file)
{
c1.clear();
c1.open(file.c_str(),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();
}

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧