Lion1942 Lion1942
关注数: 5 粉丝数: 30 发帖数: 1,065 关注贴吧数: 11
求大神帮忙啊,搞不定啊! 预先的打算是这样的: 1.建立MFC单文本程序 2.从.txt文件中读取文本文件到string对象,然后保存于容器 3.将读取到的文本文件按顺序显示到主界面 4.用户可以使用鼠标点击的方式选中一行或多行,只点击选择,不需要其他操作 5.获取用户选择的行,从容器中删除这些行 6.将处理后的文本文件重新写回到.txt文件中 现在的问题: 1.读取文件,保存文件已经完成 2.使用CListBox的方式显示读取到的文件 3.为了处理窗口重绘的问题,将数据显示的步骤写在了视类的OnDraw函数中,但是会出现Debug错误 以下为源码,请各位帮忙看下存在的问题,或者有其他比较好的解决方案的,也请帮忙提出来,万分感谢! 注: m_pListBox定义为视类的成员,在构造函数中进行初始化: CPlatFormView::CPlatFormView() { // TODO: 在此处添加构造代码 m_pListBox = new CListBox(); } void CPlatFormView::OnDraw(CDC* pDC) { CPlatFormDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此处为本机数据添加绘制代码 if (!m_vecBuffer.empty()) { //按行输出容器内数据到主界面 CRect rect; GetClientRect(&rect); m_pListBox->Create(LBS_MULTIPLESEL | WS_VSCROLL | WS_HSCROLL, rect, this, IDC_AWCLEAR_LISTBOX); m_pListBox->InitStorage(400, 10230); m_pListBox->ShowWindow(SW_SHOWNORMAL); /*this->SetHorizontalExtent(1000);*/ vector<DATA>::iterator beg = m_vecBuffer.begin(); while (beg != m_vecBuffer.end()) { string strUTF8 = beg->sData; string strAnsi = CCharacterChange::UTF8_To_STRING(strUTF8); wstring wsUnicode = CCharacterChange::AnsiToUnicode(strAnsi); m_pListBox->AddString(wsUnicode.c_str()); beg++; } } }
模板类重载后置++运算符不起作用... 今天写了个迭代器的模板类,其中重载了后置++,可是写完后发现不起作用,代码如下,麻烦各位帮忙瞧瞧是什么原因! //迭代器类 #ifndef ITERATOR_H #define ITERATOR_H template<class T>class Iterator{ public: Iterator():p(0){}; Iterator(T* point):p(point){}; ~Iterator(){}; Iterator(const Iterator<T>& c):p(c.p){}; Iterator<T>& operator=(const Iterator<T>& c); T operator*(); Iterator<T>& operator++(); //前置++,-- Iterator<T>& operator--(); const Iterator<T> operator++(int); //后置++,-- const Iterator<T> operator--(int); const Iterator<T> operator+(int a); private: T* p; }; template<class T> Iterator<T>& Iterator<T>::operator=(const Iterator<T>& c) { if(p!=c.p) delete p; p=c.p; return *this; }; template<class T> T Iterator<T>::operator*() { return (*p); }; template<class T> Iterator<T>& Iterator<T>::operator++() { p++; return *this; }; template<class T> Iterator<T>& Iterator<T>::operator--() { p--; return *this; }; template<class T> const Iterator<T> Iterator<T>::operator++(int) //后置++ { Iterator it(*this); it.p++; return it; }; template<class T> const Iterator<T> Iterator<T>::operator--(int) //后置-- { Iterator it(*this); it.p-=1; return it; }; template<class T> const Iterator<T> Iterator<T>::operator+(int a) { Iterator<T> it(*this); it.p+=a; return it; }; #endif #include "Iterator.h" #include <iostream> using namespace std; int sum(Iterator<int> begin,Iterator<int> end) //累加函数 { int result=0; while(begin!=end) { result+=*begin; begin++; //此处++根本不起作用 } return result; }; int main() { int numbers[]={1,2,3,4,5,6,7,8,9,10}; Iterator<int> it(numbers); cout<<"The result is: "<<sum(it,it+10)<<endl; }
大神帮忙啊! 自己写了个模拟迭代器,用vector容器时可以通过.begin()获取到vector容器内对象的指针,为什么用list容器时无法获取到其对象的指针?代码如下,求各位大神们帮忙分析下,小弟不胜感激!#include <iostream>#include #include <list>#include #include <algorithm>#include using namespace std;template class box{ //////////////////boxprivate:iter height;iter length;iter wideth;public:box(iter heightvalue=0,iter lengthvalue=0,iter widethvalue=0);box(const box& abox);iter volume();box& operator=(const box& abox);bool operator<(const box& abox);iter getheight()const{return height;};iter getlength()const{return length;};   iter getwideth()const{return wideth;};void setheight(iter heightvalue)const{height=heightvalue;};   void setlength(iter lengthvalue)const{length=lengthvalue;};void setwideth(iter widethvalue)const{wideth=widethvalue;};};templatebox::box(iter heightvalue,iter lengthvalue,iter widethvalue):height(heightvalue),length(lengthvalue),wideth(widethvalue){};templatebox::box(const box& abox){height=abox.height;length=abox.length;wideth=abox.wideth;};templateiter box::volume(){return height*length*wideth;};templatebox& box::operator=(const box& abox){   this->setheight(abox.getheight())const;this->setlength(abox.getlength())const;this->setwideth(abox.getwideth())const;return *this;};templatebool box::operator<(const box& abox){return this->voluem() ////////////////cartonclass carton:public box{private:inter pmentral;public://carton(inter pstr="paple");carton(iter heightvalue=0,iter lengthvalue=0,iter widethvalue=0,inter pstr="paple"); carton(const carton& acarton);   carton& operator=(const carton& acarton);inter getmentral()const{return pmentral;};void setmentral(inter pstr)const{pmentral=new inter[strlen(pstr)+1];strcpy(pmentral,pstr);};};/*templatecarton::carton(inter pstr){pmentral=new inter[strlen(pstr)+1];strcpy(pmentral,pstr);};*/templatecarton::carton(iter heightvalue,iter lengthvalue,iter widethvalue,inter pstr):box(heightvalue,lengthvalue,widethvalue){   pmentral=new char[strlen(pstr)+1];strcpy(pmentral,pstr);};templatecarton::carton(const carton& acarton):box(acarton){   pmentral=new char[strlen(acarton.getmentral())+1];strcpy(pmentral,acarton.getmentral());};templatecarton& carton::operator=(const carton& acarton){   this->setheight(acarton.getheight())const;this->setlength(acarton.getlength())const;this->setwideth(acarton.getwideth())const;this->setmentral(acarton.getmentral())const;return *this;};template ///////////////////iterclass iters:public iterator{private:intel aintel;public:iters(intel tintel);iters(const iters& titer);~iters(){};iters& operator=(const iters& titer);bool operator<(const iters& titer);   intel operator*();intel operator[](size_t index);iters& operator++();   iters& operator--();   bool operator!=(const iters& tintel)const;intel getaintel()const{return aintel;};void setaintel(intel tintel){aintel=tintel;};};templateiters::iters(intel tintel){aintel=tintel;};templateiters::iters(const iters& titer){aintel=tintel.aintel;return *this;};templateiters& iters::operator=(const iters& aiter){   this->setaintel(aiter.
在线等,帮忙看看啊 如下定义了两个类,一个基类,一个派生类,然后定义了一个拍成类对象的链表,但是当使用getline(cin,astring)函数输入string成员时无法读取(getline第三个参数为默认),但当把getline()函数第三个参数设置为某一个字符(如:getline(cin,astring,'#')时,可以读入,为什么? class box{ protected:      double height;      double length;      double wideth; public:      box(double heightvalue,double lengthvalue,double widethvalue);      box();      box(const box& abox);      box& operator=(const box& abox);      virtual   double volume();      bool operator<(box& abox);      friend double getwideth(box& abox);      static double getlength(box& abox);      double showvolume();      double getheight()const{return height;};      double getlength()const{return length;};      double getwideth()const{return wideth;}; }; class carton:public box{      public:      string* pmentral;      carton* next;      double getheight();      carton(string pstr="paple");      carton(double heightvalue,double lengthvalue,double widethvalue,string pstr="paple",carton* next=NULL);      carton(const carton& acarton);      void getmentral(const carton& acarton);      double volume();      double showvolume();      carton* createlist(); }; carton* carton::createlist() {      carton* head=NULL;      carton* end=head;      carton* ps;      char ch;      cout<<"Please input the value of the carton!"<<endl;      string astring;      do{                   double heightvalue=0;          double lengthvalue=0;          double widethvalue=0;                   cin>>heightvalue;          cin>>lengthvalue;          cin>>widethvalue;                   getline(cin,astring);                                 //此处为何读取不了输入的字符串???          cout<<astring;          ps=new carton(heightvalue,lengthvalue,widethvalue,astring);          if(head==NULL)          {              head=ps;          }          else          {              end->next=ps;          }          end=ps;                   cout<<"Do you want to input again?"<<endl;          cin.ignore(1024,'\n');          cin>>ch;          cout<<endl;      }while(ch=='y'||ch=='Y');      end->next=NULL; //     ps->next=NULL; //     delete ps;      return head; };
高手看看什么原因 #include <iostream> using namespace std; class time{ public:      int day;      int month;      int year;      time(int dayvalue,int monthvalue,int yearvalue);      time();      //time2();      int getday(){return day;};      int getmonth(){return month;};      int getyear(){return year;}; }thisday,thatday,thirdday; int main() {      time thisday(30,12,2010);      time *pthisday=&thisday;      time time[5];      time[0]=thisday;      cout<<"This day is:"          <<endl          <<time->getyear()          <<" years "          <<endl          <<time->getmonth()          <<" months "          <<endl          <<time->getday()          <<" days "          <<endl;      time thatday(24,10,2012);      //thatday.day=24;      //thatday.month=10;      //thatday.year=2012;      time[1]=thatday;      cout<<"Thatday is : "          <<endl          <<(time+1)->getyear()          <<" years "          <<endl          <<(time+1)->getmonth()          <<" months "          <<endl          <<(time+1)->getday()          <<" days "          <<endl;      cout<<"This day is : "          <<endl          <<pthisday->getyear()          <<" years "          <<endl          <<pthisday->getmonth()          <<" months "          <<endl          <<pthisday->getday()          <<" days "          <<endl;      return 0; }      time::time(int dayvalue,int monthvalue,int yearvalue) {      cout<<"This program has be repalyed!"          <<endl;      day=dayvalue;      month=monthvalue;      year=yearvalue; }      time::time()      {          cout<<"This program has be replayed!"              <<endl;          day=month=year=1;      } --------------------Configuration: 对象 - Win32 Debug-------------------- Compiling... 对象.cpp D:\VC文件\对象.cpp(33) : error C2146: syntax error : missing ';' before identifier 'thatday' D:\VC文件\对象.cpp(33) : error C2064: term does not evaluate to a function 执行 cl.exe 时出错. 对象.obj - 1 error(s), 0 warning(s)
1 下一页