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

学校作业急编译时老是抱错,又看不懂,求高手帮忙解决办法

2012-03-08 
学校作业急,编译时老是抱错,又看不懂,求高手帮忙//(5.9.11)/*Readasequenceofwordsfrominput.UseQuitasawo

学校作业急,编译时老是抱错,又看不懂,求高手帮忙
//(5.9.11)
/*
Read   a   sequence   of   words   from   input.   Use   Quit   as   a   word   that   terminates   the   input.
Print   the   words   in   the   order   they   were   entered.   Don 't   print   a   word   twice.   Modify  
the   program   to   sort   the   words   before   printing   them.
*/
/*
从键盘读入若干单词,Quit作为结束符,按顺序打印,别打重复的。打印前对单词进行排序
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using   namespace   std;
class   VString{
public:
void   push(string);
void   sortA();
void   dump_all();
private:
bool   cmpA(const   char*,const   char*);
bool   cmp(const   string,const   string);
void   dump(string);
vector <string>   vs;
};
int   main(){
VString   vs;
string   temp;
while(cin> > temp,temp!= "Quit ")
vs.push(temp);
vs.sortA();
vs.dump_all();
system( "pause ");
return   0;
}
void   VString::push(string   s){
if(vs.end()==find(vs.begin(),vs.end(),s))
vs.push_back(s);
}
void   VString::sortA(){
std::sort(vs.begin(),vs.end(),&VString::cmp);//这里报错  
}
void   VString::dump_all(){
std::for_each(vs.begin(),vs.end(),&VString::dump);//这里报错  
}
bool   VString::cmpA(const   char*   s1,const   char*   s2){
if(s1[0]== '\0 ')   return   true;
if(s1[0]==s2[0])
return   cmpA(s1+1,s2+1);
return   s1[0] <s2[0];
}
bool   VString::cmp(const   string   s1,const   string   s2){
        return   cmpA(s1.c_str(),s2.c_str());
}
void   VString::dump(string   s){
cout < <s < <endl;
}
//不知道怎么改  


[解决办法]
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;


class VString{
public:
void push(string);
void sortA();
void dump_all();
private:
static bool cmpA(const char*,const char*); //加上static
static bool cmp(const string,const string); //加上static
static void dump(string); //加上static
vector <string> vs;
};


int main(){
VString vs;
string temp;
while(cin> > temp,temp!= "Quit ")
vs.push(temp);

vs.sortA();
vs.dump_all();
system( "pause ");
return 0;
}

void VString::push(string s){
if(vs.end()==find(vs.begin(),vs.end(),s))
vs.push_back(s);
}

void VString::sortA(){
sort(vs.begin(),vs.end(),&VString::cmp);//这里报错
}

void VString::dump_all(){
for_each(vs.begin(),vs.end(),dump);//这里报错
}

bool VString::cmpA(const char* s1,const char* s2){
if(s1[0]== '\0 ') return true;
if(s1[0]==s2[0])
return cmpA(s1+1,s2+1);
return s1[0] <s2[0];
}

bool VString::cmp(const string s1,const string s2){
return cmpA(s1.c_str(),s2.c_str());
}


void VString::dump(string s){
cout < <s < <endl;
}



自己对照!

热点排行