在teseclass文件用到输入运算符时,出现如下错误:
1为什么会显示找不到文件?2第二个错误我查了一下好像是要重载运算符?如果是的话,我没有学过这个知识,现在不知道该怎么处理。
请指点要如何操作!
代码如下:
wordcount.h
#ifndef WORDCOUNT_H
#define WORDCOUNT_H
# include<iostream>
# include<stdio.h>
#include<string>
#include <vector>
#include <iomanip>
using namespace std;
class Wordcount
{
public:
Wordcount();
string getword();
void removesep();
void removeword();
void saveword();
void descendingsort();
void showwordset();
private:
int pos;//标点符号的位置
int size;//向量的size
string word;//缓存字符
string sep;//标点符号
vector<int> word_num;//单词的频率统计
vector<string> stop_word;//应删去的词
vector<string> word_set;//确认不是stopword词汇后的单词放进去该向量
};
bool flag1,flag2;
#endif
wordcount.cpp
# include<iostream>
# include<stdio.h>
#include<string>
#include <vector>
#include <iomanip>
#include"wordcount.h"
using namespace std;
Wordcount::Wordcount()
{
pos=0;
size=0;
string sep(" ,.?!:\"\t\n();");//分隔符号
vector<int> word_num;//单词的频率统计
vector<string> stop_word;//应删去的词
vector<string> word_set;//确认不是stopword词汇后的单词放进去该向量
stop_word.push_back("to");
stop_word.push_back("in");
stop_word.push_back("will");
stop_word.push_back("of");
stop_word.push_back("be");
stop_word.push_back("and");
stop_word.push_back("a");
stop_word.push_back("the");
}
string Wordcount::getword()
{
return word;
}
void Wordcount::removesep()
{
// 除去每一个word多余的符号
pos = word.find_first_of(sep);
if ( pos == word.size()-1) //符号出现在尾部
{
string tmp(word.substr(0, pos));
word = tmp;
}
else if (pos == 0)//符号出现在头部
{
string::size_type pos2 = word.find_first_not_of(sep, 1);
pos = word.find_first_of(sep, pos2+1);
string tmp(word.substr(pos2, pos-1));
word = tmp;
}
}
void Wordcount::removeword()
{
// 除去stop word
flag1=false;
for (int i = 0; i < stop_word.size(); i++)
{
if (word==stop_word[i])
{
flag1=true;
}
}
}
void Wordcount::saveword()
{
//判断单词集是否存在单词或者与word相同的单词
size = word_set.size();
flag2 = true;
for (int i=0; i!=size; ++i)
if (word_set[i] == word) {
++word_num[i];
flag2 = false;
break;
}
//存入单词集并在次数向量中对应+1
if (flag2)
{
word_set.push_back(word);
word_num.push_back(1);
}
}
void Wordcount::descendingsort()
{
//给单词次数进行降序处理,同时改变单词顺序
for (int i = 0; i < word_set.size()-1; i++)
{
int currentmax = word_num[i];
int currentmaxindex = i;
string temps_storage;
for (int j=i+1; j < word_set.size(); j++)
{
if (word_num[j]>currentmax)
{
currentmax = word_num[j];
temps_storage = word_set[j];
currentmaxindex = j;
}
}
if (currentmaxindex!=i)
{
word_num[currentmaxindex] = word_num[i];
word_num[i]=currentmax;
word_set[currentmaxindex] = word_set[i];
word_set[i] = temps_storage;
}
}
}
void Wordcount::showwordset()
{
// 输出单词以及对应的频率
for (int i = 0; i < word_num.size(); i++)
{
cout<<word_set[i]<<" "<<word_num[i]<<endl;
}
}
testclass.cpp
#include "wordcount.h"
# include<iostream>
# include<stdio.h>
#include<string>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
Wordcount wordcount;
cout<<"please enter the text"<<endl;
while (cin >> wordcount.getword())
{
wordcount.removesep();
wordcount.removeword();
if (flag1)
{
continue;
}
wordcount.saveword();
wordcount.descendingsort();
wordcount.showwordset();
}
}
整个main都是错误啊
int main()
{
Wordcount wordcount;
cout<<"please enter the text"<<endl;
while (cin >> word)
{
removesep();
removeword();
if (flag1)
{
continue;
}
saveword();
descendingsort();
showwordset();
}
}